Extract a word

This is a discussion on Extract a word within the C++ Programming forums, part of the General Programming Boards category; Hi all, this is my first post on the forum and I hope I can get some help with my ...

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    4

    Extract a word

    Hi all, this is my first post on the forum and I hope I can get some help with my problem.
    I need to make a program to extract the last word from a sentence. I managed to extract the last character, but I have no idea how to get a word.
    Code:
    #include <iostream> 
    #include <string> 
    using namespace std; 
    int main() 
    { 
     string strSentence = "Today will rain";
     int intLength;
     intLength = strSentence.length();
     cout << "\nThe last character of " << strSentence 
          << " is a " << "'"  << strSentence.substr(intLength - 1,1) << "'" ;
     cout << endl; 
     system("PAUSE"); 
     return 0; 
    }//end main
    Any help is appreciated!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So do the same thing, except start at the first character of the last word. Note also that string has member functions that can find, say, the last space in the word.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Yes, I know how to get a character or a word for a given sentence
    Code:
    #include <iostream> 
    #include <string> 
    using namespace std; 
    int main() 
    { 
     string strSentence = "Today will rain";
     int intLength;
     intLength = strSentence.length();
     cout << "\nThe last character of " << strSentence 
          << " is a " << "'"  << strSentence.substr(intLength - 4,4) << "'" ;
     cout << endl; 
     system("PAUSE"); 
     return 0; 
    }//end main
    this will return "rain", but if the sentence is unknown, then I run into problems.
    Your other suggestion with the last space sounds like it might work , only if I knew what function you are talking about!

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That's why they have (a) books (b) informational pages like cplusplus.com (c) google.

  5. #5
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    I thought that it may had something to do with the last space and I had used option (c), but the results
    Code:
    in.find_last_of(" ")
    
    or
    
    rfind(' ');
    are too advanced for what I need to use at the moment and for my knowledge.
    Anyway, thanks for your advices.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,047
    Words are how C++ naturally breaks up streams when read into strings.

    So in this case you can use an output string stream to read word by word. You can use "mysstream >> word" or a stream iterator. You will then need to strip punctuation, which to do well would require a dictionary, but can be done reasonably well by stripping everything at the beginning and end of words.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    493
    by teh way guys , does any one know why this doesnt work?
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "linescanner.h"
    #include <string>
    #include <cctype>
    #include <cstring>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
           int choice = 0, found1=0, check=0, found2=0;
        
           string str ("loop : RED X num");
           char * cstr;
           int k = str.length();
           char c; 
           found1=str.find(" X ");
           found2=str.find(" : "); 
    
           cstr = new char [str.size()+1];
           strcpy (cstr, str.c_str());
           c = cstr[k-1];     
               
            if ( found1 !=-1 ) //if the " X " is found then proceed to the next step 
            {
                     
    
                    if (found2 !=-1) //if " : " (the colon is found ) then >> next step
                    {
                            cout<<"found "<<found2<<endl;
                            if (isalpha(c))
                            {
                                    
                                    check = 15;
                                    cout<<"check"<<check;
                                    //return 15;      //its a  command with X and string offset ( RED X num)
                            }
                    }
    
    
    
            }
            cout<<check;
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    Last edited by Masterx; 01-22-2009 at 01:09 AM.
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
    Bill Bryson


  8. #8
    Registered User
    Join Date
    Jan 2009
    Posts
    4
    Quote Originally Posted by King Mir View Post
    Words are how C++ naturally breaks up streams when read into strings.

    So in this case you can use an output string stream to read word by word. You can use "mysstream >> word" or a stream iterator. You will then need to strip punctuation, which to do well would require a dictionary, but can be done reasonably well by stripping everything at the beginning and end of words.
    Thanks for advice, I'll see what I can do with the new load of info!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  3. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  4. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  5. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 05:44 PM

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