How do I take apart a string and get individual characters/words?

D

derrick

I used to program text RPG's in C back in high school, but I was very crude
at it and only learned what I needed to get the job done. I am now about to
graduate college with a degree in English and have had an interest in the
structure of language. In order to begin with my little program, I need to
be able to take a string like "The dog jumped." and be able to take the
first word from the sentence and assign it to a string, such as word1, then
skip the blank space and assign dog to the string word2 and then skip once
again and assign jumped to the string word3. I would give you some code I
was working with to try to do this, but I would rather save your stomachs
from hurting too much.... the laughter would be too much to bear =) But I
have attempted it on my own, and just need a little jump in the right
direction, if not just an all out solution. I appreciate any help you can
provide.
 
M

Mysidia

So you want to tokenize the string.
One quick approach, however, is to use sstream:

#include <sstream>
#include <string>
#include <iostream>

int main()
{
std::istringstream s;
std::string w1, w2;

s.str("A b c");
s >> w1 >> w2;
std::cout << "word1 = "<< w1 << "\nword2 = " << w2 << "\n";
}


To really tokenize a C++ string, you'll need to write your own
function, however.

to get at the individual characters, you can use the subscript
operator, i.e.

string s = "_lah";
s[0] = 'b';
changes s to "blah"

So another possibility is to read the string and build a list of
words...

string stringVar = "blah blah blah";
vector <string> words;

for(pos = last = 0; pos < stringVar.length(); pos++) {
if (stringVar[pos] == ' ') {
words.pushBack(stringVar.substr(last, pos-lst));
last = pos+1;
}
}
 
J

Jonathan Mcdougall

derrick said:
I used to program text RPG's in C back in high school, but I was very crude
at it and only learned what I needed to get the job done. I am now about to
graduate college with a degree in English and have had an interest in the
structure of language. In order to begin with my little program, I need to
be able to take a string like "The dog jumped." and be able to take the
first word from the sentence and assign it to a string, such as word1, then
skip the blank space and assign dog to the string word2 and then skip once
again and assign jumped to the string word3. I would give you some code I
was working with to try to do this, but I would rather save your stomachs
from hurting too much.... the laughter would be too much to bear =) But I
have attempted it on my own, and just need a little jump in the right
direction, if not just an all out solution. I appreciate any help you can
provide.

I'd be tempted to say "buy a book". If you talk about such variables
(you seem not to know about arrays) and if you don't even know how to
parse a string, then I suggest you try to learn the language with simple
programs first.

Here's something to get you started with your problem.

# include <string>
# include <iostream>

int main()
{
std::string s = "The dog jumped.";

std::string::size_type start=0, end=0;

start = s.find_first_not_of(" ");

if ( start == std::string::npos )
{
std::cout << "not found";
return 0;
}

end = s.find_first_of(" ", start);

if ( end == std::string::npos )
{
std::cout << "not found";
return 0;
}

std::cout << "first word: " << s.substr(start, end - start);
}

Look up std::string's member functions and std::vector for a collection
(such as for putting the individual words).


Jonathan
 
M

Mike Wahler

derrick said:
I used to program text RPG's in C back in high school, but I was very crude
at it and only learned what I needed to get the job done. I am now about to
graduate college with a degree in English and have had an interest in the
structure of language. In order to begin with my little program, I need to
be able to take a string like "The dog jumped." and be able to take the
first word from the sentence and assign it to a string, such as word1, then
skip the blank space and assign dog to the string word2 and then skip once
again and assign jumped to the string word3. I would give you some code I
was working with to try to do this, but I would rather save your stomachs
from hurting too much.... the laughter would be too much to bear =) But I
have attempted it on my own, and just need a little jump in the right
direction, if not just an all out solution. I appreciate any help you can
provide.

#include <algorithm>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

int main()
{
std::string s("The dog jumped over the cat");
std::vector<std::string> words;

std::istringstream iss(s);
std::copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(words));

std::cout << "string: " << s << "\n\n"
<< "words:\n";

std::copy(words.begin(), words.end(),
std::eek:stream_iterator<std::string>(std::cout, ", "));

std::cout << "\n\n";

std::string word(words[2]);
std::cout << "Third word: " << word << "\n\n";

std::vector<char> w(words[2].begin(), words[2].end());

std::copy(std::istream_iterator<char>(iss),
std::istream_iterator<char>(),
std::back_inserter(w));

std::cout << "Characters of \"" << word << "\"\n";

std::copy(w.begin(), w.end(),
std::eek:stream_iterator<char>(std::cout, ", "));

std::cout.put('\n');
return 0;
}

-Mike
 
D

derrick

You have all been very helpful, except perhaps the first responder... I
appreciate all the help you have provided =) and my program is well on its
way since I last spoke to you all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top