How to store string words into individual variable

For example:

string str1(“Today is a very nice day!”);

then we need to store

string s1 (“Today”);
string s2(“is”);
string s3(“very”);
string s4(“nice”);
string s5(“day”)

Thanks a lot!

Recommended Answers

All 4 Replies

Here is something I wrote for my friend when he was going to school and it worked for him. I'll leave it up to you to figure out what you need or don't need.

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

using namespace std;

int main()
{
	string sentence, word, longest, shortest, charString, wordString;
	int characters, words;
	vector<string>wordlist;
	
	cout << "Please enter some words (ctrl-d or ctrl-z to quit): " << endl;
	
	while(getline(cin, sentence))
	{
		wordlist.clear();
		characters = sentence.length();
		int lastSpace = -1;
		
		for( int i = 0; i < sentence.length(); i++ )
		{
			if( sentence[i] == ' ' )
			{
				word = sentence.substr(lastSpace+1, (i-lastSpace-1));
				wordlist.push_back(word);
				lastSpace = i;
				characters--;
			}
			else if( i == sentence.length()-1 )
			{
				word = sentence.substr(lastSpace+1, (i-lastSpace));
				wordlist.push_back(word);
			}
		}
		
		words = wordlist.size();
		
		switch(characters)
		{
			case 1:
				charString = "character";
				break;
			default:
				charString = "characters";
				break;
		}
		
		switch(words)
		{
			case 1:
				wordString = "word";
				break;
			default:
				wordString = "words";
				break;
		}
		
		for( int i = 0; i < wordlist.size(); i++ )
		{	
			if( i == 0 )
			{
				longest = wordlist[i];
				shortest = wordlist[i];
			}
			if( wordlist[i].length() > longest.length() )
			{
				longest = wordlist[i];
			}
			if( wordlist[i].length() < shortest.length() )
			{
				shortest = wordlist[i];
			}
		}
		
		if( words > 0 )
		{
			cout << "You entered " << words << " " << wordString << " totalling "<< characters << " " << charString << "." << endl;
			cout << "The longest was \"" << longest << "\"." << endl;
			cout << "The shortest was \"" << shortest << "\"." << endl << endl;
		}
		else
		{
			cout << "You didn't enter any words." << endl << endl;
		}
		
	}
    system("PAUSE");
    return 0;
}

There are many ways, I will show you one way :

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

using namespace std;
 

void extractWords(string source, string * wordArray, const int size)
{
	stringstream seperate; //our stream object that will extract each word

	seperate << source; //insert what we want to extract

	int i = 0;
	//extract each word by word making sure not to go over bounds
	while(i != size && seperate >> wordArray[i]) 
		i++;

}
int main()
{		
	string phrase("To kill or not to kill");
	
	const unsigned short MAX_SIZE = 6;
	
	string words[MAX_SIZE];

	extractWords(phrase,words,MAX_SIZE);

	for(int i = 0; i < MAX_SIZE; i++){
		cout<<words[i]<<endl;
	}


	return 0;
	
}

I am not gonna give you the exact code to do it, just an idea...

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
 char s1[]="Today is a very nice day!!";
 char* pch;
cout<<"Split tokens.."<<endl;
pch=strtok(s1," ");//This is the function to use for breaking strings
while(pch!=NULL){
cout<<pch<<endl;
pch=strtok(NULL," ");
}
return 0;
}

SO this should help, if you compile and run this code, you will see the string broken. then the only problem left now is how to store these broken strings into s2,s3,etc. I would leave that for you to do.
I have tried.
If this helped, please mark this thread as solved and add to my reputation points...Thanks!!

"Show me your code and I will tell you who you are.."-Tkud

Yeah. put them in the stringstream, split it with getline, and strore the results on a vector.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.