My question is this... I want to be able to enter a string into my program, like "The dog jumped." Then I want the program to be able to separate the 3 separate words... How can I do this?
My question is this... I want to be able to enter a string into my program, like "The dog jumped." Then I want the program to be able to separate the 3 separate words... How can I do this?
The string library has some pretty handy stuff for doing just that. Include <string> in your source code. Look up the function strtok( ). Basically it takes 2 parameters. The first is a pointer to a character string that you want to "tokenize". For example, your first argument might be "Hello World". The next argument is the delimeter. This is probably going to be " " just a whitespace string. The function will return to you the first token it finds as a pointer to a character string. If it does not find a valid token, it will return null. When you call the function again to get the next token (i.e. world in our example) you pass NULL as the first parameter. If you need some source or something just let me know.Originally posted by blackwyvern
My question is this... I want to be able to enter a string into my program, like "The dog jumped." Then I want the program to be able to separate the 3 separate words... How can I do this?
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
You could just make up an algorithm yourself.
KuphrynCode:#include <vector> #include <string> std::string szText = "Testing 1 2 3"; std::vector<std::string> szWordList; int indexA = 0, indexB = 0; while (indexB != -1) { indexB = szText.find(' ', indexA); if (indexB != -1) { szWordList.push_back(szText.substr(indexA, indexB - indexA ++indexB; indexA = indexB; } }
Simple source code to your problem. Not very ellegant but I whipped it up pretty quick.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
I you don't have the STL available in your compiler, then the string and vector classes as well as the find() algorhythm won't be available to you.
strtok() works on null terminated strings (too).
you can write your own function looking for space char without using the find algorhythm. Just evaluate each char in the input buffer to see if it is a space. If it isn't transfer the char to a holding buffer. if it is transfer a NULL char to the holding buffer. Transfer the data in the holding buffer somewhere else and go on to the next char in the input buffer.
I wouldn't recommend using strtok because it's almost
as complicated has doing it by hand and is kind of strange
since it uses a static variable.
By hand you can do something
like
Or if you like c you can use fgetc and ungetc. Since mostCode:void skip_ws(ifstream& in) { int c; while((c = in.get()) && isspace(c)) continue; in.putback(c); }
of the manipulation is going to be so low level
this is what I would do.
Once your done this in a loop you can call skip_ws.
Because you have skipped all the white space you know that the next charecter read in is going to
be part of a token but you could have a situation like
c = foo+5; where your program would tokenize
it as
'c' '=' 'foo+5;' so you will have to add more code to tokenize
it like 'c' '=' 'foo' '+' '5'
While you are tokenizing it you will want to decide that
'c' is a indentifier '=' is an operator 'foo' is an indentifier and
'5' is a numerical constant.
I guess you won't have to worry about compiling
the dog jumped! Maybe I've been studying too long.
Use the cin.getline function to read an entire line.
char* buffer;
cin.getline(buffer, 40, '\n');
Reads up to 40 chars and stops at the newline character.