Could please help me in the code,of how to extract the no of words in each sentence(if there are 2 or more sentence).But i want the code to use string function?????????

if you post the code that YOU have tried then we can help you in debugging and solving the problem. but you cant just expect spoon feeding here..

ps: i just got ripped left-right-center for trying to solve a similar thread

I read abt token function but i dnt knw how to use it.please help?????????

int a,b=0;

for(a=0;a<sr1.length();a++)
{
if (st1[a]
{
b++;
}
}

cout<<endl;
cout<<"number of words in a sentence<<endl;
cout<<endl;

if you post the code that YOU have tried then we can help you in debugging and solving the problem. but you cant just expect spoon feeding here..

ps: i just got ripped left-right-center for trying to solve a similar thread

I read abt token function but i dnt knw how to use it.please help?????????

int a,b=0;

for(a=0;a<sr1.length();a++)
{
if (st1[a]
{
b++;
}
}

cout<<endl;
cout<<"number of words in a sentence<<endl;
cout<<endl;

'strtok' returns tokens in the string. Basically you put it in a loop, give the input string and a delimiter as arguments, it'll keep returning each token. you can put a counter and check for NULL token to exit the loop. for function signature try reading some doc.

and yes make sure you read the 'things to keep in mind' while using strtok or the result would be unexpected... dont want to tell you and spoil the suspense :)

It is easier to use std::stringstream class to do that

#include <sstream>
<other includes here>

...
string sentence = "Once upon a time there were three little pigs";
string word;
vector<string> array;
stringstream stream(sentence);
while( stream >> word)
{
    array.push_back(word);
}
This article has been dead for over six months. Start a new discussion instead.