-
istringstream
I have managed to tokenize my input lines. if i specifically want the 1st & last word & no. of words typed to be printed , how do i code it? I've already tried find_first_of(), but to no good progress. can someone help?
Code:
2 #include <sstream>
3 #include <string>
4 #include <iostream>
5 using namespace std;
6
7 int main ()
8 {
9 string s;
10 char str[100];
11 while (getline(cin,s))
12 {istringstream is(s);
13 while(is >> str)
14 {
15
16 {
17 cout << str << endl;
18 }
19 }
20 }
21 return 0;
22 }
-
Store the tokens in a vector, then you can do this:
Code:
cout<<"First: "<< v.front()
<<"Last: "<< v.back()
<<"Size: "<< v.size();
-
>> while(is >> str)
Why is str a character array? You can (and should) make it a string, too.
-
initially i tot tt i need to make string of words(sentences) into an array tt's why put as char. but end up frustrated bcos only print 1st char of every word. now trying vectors for the 1st time, thanks for the tip. will try harder.
-
>will try harder.
Please try harder with your posts as well. I'm generally pretty good at reading incomprehensible abbreviations, and I found your post difficult to follow. Imagine what it's like for people who aren't fluent in English or familiar with common abbreviations. Proper spelling and grammar means that more people will be available to help you.
-
-
thank you
thank you prelude and daved, you have made my hours of learning fruitful.