reading strings and ignoring symbols

I am trying to do this:

a user would input:

SEND + MORE = MONEY

I want to read the three words without the '+' and '=' and put them into string1, string2, string3..
then store them as characters in 3 different vectors.

how can I do that?

Last edited on
This is called "tokenising".

If you do a search for "C++ tokenise string", you'll find lots of examples.
Hi Moschops,

I searched for tokenizing and I did the following.

But I don't understand how is this different from just plugging a string into a vector.

I know I didn't do something, but what is it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <sstream>
#include <iterator>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string userInput = "Send + More = Money";
	istringstream iss(userInput);
	vector<string>tokens
	{
		istream_iterator < string > {iss},
		istream_iterator < string > {} 
	};

	for (vector<string>::const_iterator i = tokens.begin(); i != tokens.end(); ++i)
	{
		cout << *i << ' ';
	}

	cout << endl;
	cout << endl;
	system("PAUSE");
	return 0;
}


1
2
Output:
Send + More = Money
Without using any built-in algorithms, you can do this to separate the sub-strings:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cctype>
#include <vector>
#include <cstdlib>
using namespace std;

int main()
{
    string str, temp;
    vector<string> strings;
    bool occurring = false; // true if a string is currently occurring

    getline(cin, str);

    int sz = str.size();

    for(int i = 0; i < sz; i++)
    {
        if(!occurring && isalpha(str[i]))
        {

            occurring = true;
        }

        else if(!isalpha(str[i]) && occurring)
        {
            strings.push_back(temp);

            temp = ""; // reset

            occurring = false;
        }

        if(occurring) // if a string is currently occurring
        {
            temp += str[i];
        }

    } // end of for

    if(temp != "") strings.push_back(temp); // for the last one

    for(int i = 0; i < strings.size(); i++) cout << strings[i] << endl;

    return 0;
}


There may be a better way!
Last edited on
Hello PSYCHAMERON, that's what I was looking for.

I have a few questions about your code, can you please explain 'occurring' and also, where did you exactly ignore the signs '+' and '=' because I can't see it.

Edit: nevermind, it's the function isalpha().

Thank you.
Last edited on
isalpha is a function returns true if the character is alphabetic (a, b, c, d...z, A, B, C, D...Z). To know more- http://www.cplusplus.com/reference/cctype/isalpha/

First your original string is taken as input and saved in str. Then I traverse the whole str string from beginning to end. My target is to find a series of consecutive alphabetic characters. If I find one, I store it in the vector strings (and this is what we want). temp is used to hold this consecutive series of alphabetic characters. occurring is used to indicate whether this type of series is currently occurring inside the main loop.

If the occurring is false and the current character is alphabetic then we can toggle the mode to say that series of alphabetic character is occurring-

1
2
3
4
 if(!occurring && isalpha(str[i]))
{
    occurring = true;
}


Else if the current character is not alphabetic and the occurring is set to true, then we can say that we've found one of such a string. Then we store temp and reset it for the next use and we also set occurring to false (as current character is not alphabetic and our desired string isn't occurring)-

1
2
3
4
5
6
7
8
else if(!isalpha(str[i]) && occurring)
{
    strings.push_back(temp);

    temp = ""; // reset

    occurring = false;
}


You should understand the rest...
Last edited on
Topic archived. No new replies allowed.