469,588 Members | 2,848 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

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

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.
Jul 22 '05 #1
5 2956
For that, I would look up how to use stringstream.

Jul 22 '05 #2
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;
}
}

Jul 22 '05 #3
derrick wrote:
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
Jul 22 '05 #4

"derrick" <ra**********@yahoo.com> wrote in message
news:Q5***************@newssvr30.news.prodigy.com. ..
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::ostream_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::ostream_iterator<char>(std::cout, ", "));

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

-Mike
Jul 22 '05 #5
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.
Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by JustSomeGuy | last post: by
11 posts views Thread by Jacek Dziedzic | last post: by
6 posts views Thread by shapper | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.