Thread: Begginer Problem: Extracting words from sentence

  1. #1
    Registered User
    Join Date
    Jul 2005
    Location
    Lahore, Pakistan
    Posts
    6

    Begginer Problem: Extracting words from sentence

    Hi,

    I got an assignment from university, I have to take a sentance as input and then store each word in different nodes in a linked list (then I have to perform different actions on these words but that is not related atm)

    I have tried using getche() to store words in char array but I now don't know how to copy character array into string (to store it in Node) or any other way of extracting words from input other than using loop to find spaces and extract words and then again store them character by character in an array.

    Any help solving this problem would be much appreciated.

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Are you using C++? Are you using the C++ string class? If so, just use cin >> word to get each word separated by spaces. Even if you have to use C style strings (i.e. character arrays), you can use cin >> word.

    You can also post your attempt so we can see what exactly you are trying to do.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Location
    Lahore, Pakistan
    Posts
    6
    well, I want to store each word in seperate string (actually I have to store each word in seperate Node of linked list). That is user will input complete sentence like:

    C++ is the best language

    Now, all these words "C++", "is", "the", "best" & "language" should be stored in different nodes and to do that I need to extract these words and save them in seperate strings.

    P.S: Yes, I am using string class in C++

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    1. Create a list<string> object.
    2. Ask user for a sentence.
    3. Use getline to get the sentence from the user into a temp string.
    4. Initialize a stringstream object to the contents of the temp string.
    5. Use >> on the stringstream in a loop to get the individual words into another temp (or maybe reuse the first temp at this point) string. Push that temp string object onto the list<string>.
    6. Done!

    It's probably no more than a 10-15 line program... unless you are coding your own custom list container.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Quote Originally Posted by hk_mp5kpdw
    1. Create a list<string> object.
    2. Ask user for a sentence.
    3. Use getline to get the sentence from the user into a temp string.
    4. Initialize a stringstream object to the contents of the temp string.
    5. Use >> on the stringstream in a loop to get the individual words into another temp (or maybe reuse the first temp at this point) string. Push that temp string object onto the list<string>.
    6. Done!

    It's probably no more than a 10-15 line program... unless you are coding your own custom list container.
    That's useful for me because I was using an algorithm using a string as an array and setting iterator like variables to parse words. This is much more useful.

    Edit: Question, for the for loop, how do I check if the stringstream has not reached end of file, I tried using something like this

    Code:
    //includes for necessary standard library classes here
    int main()
    {
        string Astring = "Hello My Baby Hello My Honey";
        string temp;
        istringstream words(Astring);
        vector<string> parsed;
        while(words)
        {
            words >> temp;
            parsed.push_back(temp);
        }
        
        for(str_sz i = 0; i < parsed.size(); i++)
            cout << "*" << parsed[i] << "*" << endl;
        
        system("Pause");
        return 0;
    }
    and it works fine, but it returns the last word twice..
    Code:
    *Hello*
    *My*
    *Baby*
    *Hello*
    *My*
    *Honey*
    *Honey*
    Last edited by indigo0086; 05-04-2006 at 03:09 PM.

  6. #6
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Erp, found it, I just move the istringstream input operator in the while loop. This simplified a lot, (further studies C++ standard Library).

    got it to 9 lines of code by way of function
    Last edited by indigo0086; 05-04-2006 at 03:22 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting words from strings
    By umerkk in forum C Programming
    Replies: 4
    Last Post: 04-15-2009, 09:13 AM
  2. Reversing Letters in Words of a Sentence
    By CRSpeedy in forum C Programming
    Replies: 10
    Last Post: 03-31-2009, 06:12 PM
  3. Do { While Loop Problem - Note: Begginer Programmer
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2002, 10:28 AM
  4. Struct Problem (Begginer)
    By Venom in forum C Programming
    Replies: 1
    Last Post: 03-08-2002, 06:18 AM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM