dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: String Extract Word Function

  1. #1
    Join Date
    Aug 2019
    Posts
    6

    String Extract Word Function

    I am writing a function that accepts a string and goes until it finds the first word and puts that word into a new string and changes the old string to start at the position after the last character of the first word.

    Ex.
    string s = "***AMAZING!*** Do it, now!";
    string t = {};

    the function should return "AMAZING" and "!*** Do it, now!!"
    string t would have "AMAZING"
    string s would have "!*** Do it, now!!"

    Code:
    for (int i = 0; i < text.length; i++)
         if (isalpha(text[i]))
              t += text[i];
         else if ((isalpha(text[i])) && (!(isalpha(text[i+1]))))
              return t;
    so it returns AMAZING, but i don't know how to have it also return the changed string s

    Any help would be great, thanks!
    Last edited by 2kaud; August 4th, 2019 at 05:25 AM. Reason: Added code tags

  2. #2
    Join Date
    Aug 2019
    Posts
    6

    Re: String Extract Word Function

    the function has to accept a pass by reference string too

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,927

    Re: String Extract Word Function

    First, the code in post #1 has an out-of-bounds issue. The reference to text[i + 1] evaluates to text.length when i is 1 less than text.length (ie last time round the loop] and text[text.length] is invalid as the bounds of text are 0 to text.length - 1.

    Second, you don't define what is meant by a 'word' - a sequence of alpha characters?

    You don't specify the definition of the function, but something like this:

    Code:
    string first_word(string& text)
    {
        string t;
    ....
        // change text to start at new required position using text = text.substr(...)
        return t;
    }
    PS Whilst this is one way of doing this, it's not the recommended c++ way. That wouldn't involve a loop but use of find_if() etc. However, this probably hasn't been covered yet? One way of doing it like that would be:

    Code:
    #include <algorithm>
    #include <string>
    #include <iostream>
    using namespace std;
    
    string first_word(string& text)
    {
    	const auto f = find_if(text.begin(), text.end(), isalpha);
    	const auto l = find_if_not(f, text.end(), isalpha);
    	const auto t = string(f, l);
    
    	text.assign(l, text.end());
    	return t;
    }
    
    int main()
    {
    	string s = "***AMAZING!*** Do it, now!!";
    	string t = first_word(s);
    
    	cout << t << '@' << s << endl;
    
    	s = "***";
    	t = first_word(s);
    	cout << t << '@' << s << endl;
    }
    You also don't say what happens if a word is not found?? In the above example code, it displays:

    Code:
    AMAZING@!*** Do it, now!!
    @
    with both s and t set to empty when trying to retrieve the first word from "***" - as it doesn't contain any. This may, or may not, be what is required.
    Last edited by 2kaud; August 4th, 2019 at 06:07 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

  4. #4
    Join Date
    Aug 2019
    Posts
    6

    Re: String Extract Word Function

    Thank you for catching my error with the text.length() -1. I usually forget to add that into my code when comparing consecutive positions. And yes the definition of the function is string extractWord(string& text). And yes by word I mean a sequence of alpha characters.
    I did try to use substr by creating an int that held the position of the last character of the first word.
    text = text.substr(pos, text.size()-pos) --> but I was returning the first word before it could change the string passed through.
    And if a word is not found int the string it should set the string to an empty string.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,927

    Re: String Extract Word Function

    Code:
    text = text.substr(pos, text.size()-pos)
    Shouldn't this just be
    Code:
    text = text.substr(pos);
    which makes text just the chars from index pos to the end of the string - which is what you want.

    To see if a word has been found, then you could check if t is empty or not. If t is empty then text should be set to empty - as no word was found. Something like (not tried):

    Code:
    text = text.substr(t.empty() ? text.length() : pos);
    Last edited by 2kaud; August 5th, 2019 at 02:44 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.0)

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)