CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2007
    Posts
    7

    extract first word in a string

    user input: **HELLO**!!

    what's suppose to happen:
    firstWord = HELLO
    inspectThis = **!!

    Code:
    string extractWord (string& inspectThis)
    {
    	string firstWord;
    	int i = 0;
    
    	while (inspectThis[i] != '\n')
    	{
    		if (isalpha(inspectThis[i]))
    		{
    			firstWord += inspectThis[i];
    			inspectThis[i] = NULL;
    		}
    		inspectThis[i] = NULL;
    		i++;
    	}
    	return firstWord;
    }
    this is what i have so far and right now i'm stuck

  2. #2
    Join Date
    Feb 2000
    Location
    Indore, India
    Posts
    1,046

    Re: extract first word in a string

    Hello,

    Normally, a word is defined as the first group of characters before the first whitespace character. In your problem, you mean it as the first group of alphabets. Am I right?

    It is clear that your logic does not result in what you want. Think this way: Check for each character whether it is alphabet or non-alphabet. If you get an alphabet, add it to the first word. If you do not get an alphabet, there can be two cases: (i) you already have a word filled or (ii) you do not have a word. In the first case, terminate the function and return the word. In the second case advance to the next character.

    Here is a simple code which does what is explained above, which comes in place of the while loop in your code:

    Code:
    for (int count = 0; count < inspectThis.length(); count++)
    	if (isalpha(inspectThis[count]))
    		firstWord += inspectThis[count];
    	else if (firstWord.length() > 0)
    		break;
    Once you find the first word, you can check its position in the main string inspectThis and remove that word.

    Code:
    int pos = inspectThis.find(firstWord);
    inspectThis.erase(pos, pos + firstWord.length());
    Hope it is clear.

    Regards,
    Pravin.
    Let me know if I have helped by rating this post

    Recent FAQs

    Drag an image
    Area of a window exposed on desktop
    Display rotated bitmap

  3. #3
    Join Date
    Apr 1999
    Posts
    3,585

    Re: extract first word in a string

    Have a look at AfxExtractSubString .
    Gort...Klaatu, Barada Nikto!

  4. #4
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: extract first word in a string

    1) There is rarely a circumstance when you would set the value
    to an individual character in a std:;string to NULL. So your logic
    would be suspect right there.

    2) The algorithm to use :

    a) find the first alphabetic character.

    b) Since you only want the first word, you would start with that character
    and go until you reach a non-alphabetic character.

    3) using the standard library, the function almost writes itself (although
    the way to find the non-alphabetic character is not obvious).

    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    #include <functional>
    #include <cctype>
    
    
    std::string FirstWord(const std::string & s)
    {
        using namespace std;
    
        string::const_iterator it1 = find_if(s.begin(),s.end(),::isalpha);
        string::const_iterator it2 = find_if(it1,s.end(),not1(ptr_fun(::isalpha)));
        
        return string(it1,it2);
    }
    
    int main()
    {
        std::string s = "***!  abc  sxyz**";
    
        std::cout << FirstWord(s) << "\n";
    
        return 0;
    }
    Last edited by Philip Nicoletti; October 31st, 2007 at 06:51 AM. Reason: added :: in front of isalpha for g++ compatibility

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured