"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