CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 4 of 4

Thread: extract first word in a string

  1. #1
    Join Date
    Oct 2007

    extract first word in a string

    user input: **HELLO**!!

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

    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;
    	return firstWord;
    this is what i have so far and right now i'm stuck

  2. #2
    Join Date
    Feb 2000
    Indore, India

    Re: extract first word in a string


    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:

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

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

    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

    Re: extract first word in a string

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

  4. #4
    Join Date
    Aug 2000
    West Virginia

    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).

    #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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)