c++ - Adding the end of a string, not containing a delimiter, to a vector -


i have small program takes sentence like:

"i ,love go running!" , ignores punctuation , whitespace , counts words , displays each word line line.

this example output 5 number of words in sentence. program output each word in sentence individually.

i love go running 

the program works fine if strings ends delimiter, delimiter being of 1 of these characters: ![,?._'@+]

if strings ends without delimiter.

for example,

i love go running, pops

the program count 5 words not 6 , output

5 love go running 

the word pops ignored.

my question going on when happens, why happening?

here code:

int main() {  string s = ""; string t = "";  vector<string> words;  getline(cin,s);  int size = s.length();  for(int i=0; < size; ++i) {     if((s[i]>='a' && s[i]<='z') || (s[i]>='a' && s[i]<='z'))      {           t += s[i];                 }     else     {         if(t != "")         {              words.push_back(t);              t = "";         }     } } cout<<words.size()<<"\n";  for(vector<string>::iterator it=words.begin();it!=words.end();it++)     {      cout<<*it<<"\n";     }     return 0; } 

when loops ends, not storing t, created last word.

you should repeat following code after body of loop once more.

    if(t != "")     {          words.push_back(t);          t = "";     } 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -