I have a small problem with string. How can I make the output str to be every single string

i.e:

string str="You are a good writer";
to be:
str1="you";
str2=“are";
str3="a";
str4="good";
str5="writer";
//---------my_test_code,but not working yet.
#include <iostream>
#include <string>
using namespace std;

int main () 
{
    string str;
    getline(cin,str);
    int  i;
    string::iterator It = str.begin();
    while ( It != str.end() )
    {
        if (*It == ' ' )
            *It = '\n';

        cout <<*It++;     
    }
    cout << endl;
    return 0;
}

Edited 3 Years Ago by Nick Evan: Fixed formatting

Do you mean like this?

str1 += str2;
str1 += str3;
str1 += str4;
str1 += str5;

cout << str1;

Edited 7 Years Ago by Clinton Portis: n/a

Do you mean like this?

str1 += str2;
str1 += str3;
str1 += str4;
str1 += str5;

cout << str1;

When output str1(you are a good writer),code will define every single word to a string like str1=you,str2=are so on.

Personally I'd copy the sentence into a stringstream object and then use the stringstream's >> operator to separate each word from the sentence and store each word in a vector of strings.
Like this:

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

using namespace std;

int main()
{
	string sentence, word;
	vector<string> wordlist;
	stringstream raw_stream;

	// Get user to enter a sentence:
	cout << "Enter a sentence: ";
	getline(cin, sentence, '\n');
	cout << endl;

	// Copy the sentence into a stringstream object	
	raw_stream << sentence;

	// Separate the sentence into individual words 
	// with the >> operator and put them into the wordlist
	while(raw_stream >> word)
		wordlist.push_back(word);

	// Output the contents of the wordlist 
	if(wordlist.size()>0)
	{
		cout << "In the sentence:" << endl << sentence << endl;
		cout << "The individual words are:" <<	endl;

		int count=1;
		for(vector<string>::iterator iter = wordlist.begin(); iter!=wordlist.end(); iter++)
		{
			cout << "Word " << count << ": " << *iter << endl;;
			++count; 
		}
	}
	else
		cout << "No words were entered!" << endl;

	// wait for user to press enter
	cout << "press enter to continue..." << endl;
	getline(cin, sentence, '\n');
	return 0;
}

Cheers for now,
Jas.

Personally I'd copy the sentence into a stringstream object and then use the stringstream's >> operator to separate each word from the sentence and store each word in a vector of strings.
Like this:

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

using namespace std;

int main()
{
	string sentence, word;
	vector<string> wordlist;
	stringstream raw_stream;

	// Get user to enter a sentence:
	cout << "Enter a sentence: ";
	getline(cin, sentence, '\n');
	cout << endl;

	// Copy the sentence into a stringstream object	
	raw_stream << sentence;

	// Separate the sentence into individual words 
	// with the >> operator and put them into the wordlist
	while(raw_stream >> word)
		wordlist.push_back(word);

	// Output the contents of the wordlist 
	if(wordlist.size()>0)
	{
		cout << "In the sentence:" << endl << sentence << endl;
		cout << "The individual words are:" <<	endl;

		int count=1;
		for(vector<string>::iterator iter = wordlist.begin(); iter!=wordlist.end(); iter++)
		{
			cout << "Word " << count << ": " << *iter << endl;;
			++count; 
		}
	}
	else
		cout << "No words were entered!" << endl;

	// wait for user to press enter
	cout << "press enter to continue..." << endl;
	getline(cin, sentence, '\n');
	return 0;
}

Cheers for now,
Jas.

Thanks jas, Can I reuse the new sstrings at all? cos I need put them into mysql.
I have try your code, but not working yet.
//----------------my code
res=stmt->executeQuery("select * from test2 where english='"+??????????+"'");//I need strings in here

while (res->next())
{
cout<<"Result: "<<res->getString(2);
}
//----------------

anyway, thanks for your code.

This question has already been answered. Start a new discussion instead.