This programm counts words from input and then i have to print out the longest word, im i bit stuck on that, need to somehow loop trough all words and compare them, but thing is, that there can be whatever count of words and spaces, depends on input.
"teikums" is just my string name, it can be indefinitely long, depends on input. in this task i only have to use white spaces, no punctuations, have to enter sentence only with white spaces.
teikums=string, teikums in my language=sentence,thats just how i named my string, yes its up to user, how long the string will be, the size is not defined.you have to enter the sentence that will be stored in "teikums"
#include<iostream>
#include<cstring>
usingnamespace std;
int main()
{
string sentence;
string b;
int words, len, position, nextposition;
cout<<"Write a sentence!\n";
getline (cin,sentence);
len=sentence.length();
words=1;
for (int i=0; i<len; i++)
{
if (sentence[i]==' ') words++;
}
cout<<"There are this amount of words in a sentence: "<<words<<endl;
position=sentence.find(" ");
nextposition=sentence.find(" ", position+1);
if ((nextposition-position)>position)
system ("pause");
return 0;
}
this works,thanks, I did read the link, just im very much a beginner and dont really understand this function, as far as I understand is, istringstream copies my previous string teikums into new array and words are separated now, somehow i also have to make this display just the longest word