Hi. I was wondering if anyone would give me a hint or an idea on how to take a string and put each word in the string into the vector. How would I go about doing this?
This is a discussion on Words into a vector within the C++ Programming forums, part of the General Programming Boards category; Hi. I was wondering if anyone would give me a hint or an idea on how to take a string ...
Hi. I was wondering if anyone would give me a hint or an idea on how to take a string and put each word in the string into the vector. How would I go about doing this?
You could use strtok() within a loop to find each token in the c-string delimited by a space, and push it into the vector.
That solution may or may not be easy, but definitely not a good approach for C++.
Use std::string instead, along with find and substr.
For information on how to enable C++11 on your compiler, look here.
よく聞くがいい!私は天才だからね! ^_^
I was using std::string. The question in the excercise was:
"Read some text into a vector, storing each word in the input as an element in the vector. transform each word into uppercase letters. Print the transformed elements from the vector, printing eight words to a line."
I done it, but I think it might be a little messy.. lol.
Be gentle... very gentle.Code:#include <iostream> #include <string> #include <vector> #include <algorithm> int main() { std::vector<std::string> vText; //Container for each word std::string sText; //String of words getline(std::cin, sText); //Get input int newPos = 0; int wordCount = 0; std::string temp; while(sText.find(' ', newPos) <= sText.size()) { wordCount++; newPos = sText.find(' ', newPos) + 1; } newPos = 0; wordCount++; for(int i = 0; i < wordCount; i++) { int j = sText.find(' ', newPos); if(newPos > 0) { temp = sText.substr(newPos, j - newPos); } else { temp = sText.substr(newPos, j); } std::transform(temp.begin(), temp.end(), temp.begin(), toupper); vText.push_back(temp); newPos = j + 1; } wordCount = 0; for(std::vector<std::string>::size_type i = 0; i < vText.size(); i++) //Output each word 8 at a time { if(wordCount <= 8) { std::cout << vText[i] << " "; wordCount++; } else { std::cout << "\n" << vText[i] << " "; wordCount = 0; } } std::cout << std::endl; return 0; }
EDIT: Forgot to put them in upper case... done![]()
Last edited by gin; 06-29-2008 at 01:32 PM.
Use a stream to separate the words, since operator>> stops at whitespace by default. You can either do this when reading from cin, or you can take the line you read in and put it into a stringstream and do it in there.
>> sText.find(' ', newPos) <= sText.size()
If you were to keep this code you wouldn't want <= there, since find should never return size and will return the unsigned equivalent of -1 if it fails. I'd use while (sText.find(' ', newPos) != std::string::npos) or even while ((newPos = sText.find(' ', newPos)) != std::string::npos). However, those might not be accurate word counts if there are back to back spaces (using the stream method avoids that problem).
Last edited by Daved; 06-29-2008 at 02:29 PM.