istringstream

This is a discussion on istringstream within the C++ Programming forums, part of the General Programming Boards category; I have managed to tokenize my input lines. if i specifically want the 1st & last word & no. of ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    29

    Lightbulb 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 }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Store the tokens in a vector, then you can do this:
    Code:
    cout<<"First: "<< v.front()
        <<"Last: "<< v.back()
        <<"Size: "<< v.size();
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> while(is >> str)
    Why is str a character array? You can (and should) make it a string, too.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    29
    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.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >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.
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    29
    understood. thanks.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    29

    Lightbulb thank you

    thank you prelude and daved, you have made my hours of learning fruitful.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. istringstream problem in switch block?
    By wolfindark in forum C++ Programming
    Replies: 4
    Last Post: 06-24-2007, 11:56 PM
  2. Replies: 1
    Last Post: 10-27-2006, 01:21 PM
  3. Initializing an istringstream
    By cunnus88 in forum C++ Programming
    Replies: 2
    Last Post: 05-11-2006, 08:49 PM
  4. confused with istringstream
    By terracota in forum C++ Programming
    Replies: 4
    Last Post: 07-23-2004, 10:49 PM
  5. Istringstream fail
    By pianorain in forum C++ Programming
    Replies: 2
    Last Post: 03-09-2003, 10:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21