0

I'm trying to return the last word in a string but am having trouble with the for loops. When I try to test the function I am only getting empty strings. Not really sure what the problem is. Any help is much appreciated.

string getLastWord(string text)

{
    string revLastWord = "";
    string lastWord = "";
    if(text == "")
    {
        return text;
    }

    for(size_t i = text.size()-1; i > -1; i--)
    {

        if((isalpha(text[i])))
        {
             revLastWord+=text[i];
        }
        if(revLastWord.size()>=1 && !isalpha(text[i-1]))
        {
             break;
        }
    }

    for(size_t k = revLastWord.size()-1; k > -1; k--)
    {
        lastWord+=revLastWord[k];
    }

    return lastWord;
}
2
  • 1
    Take a look at find_last_of here cplusplus.com/reference/string/string/find_last_of you could use it to find the position of the last space in your text, which is where the last word presumably starts. Commented Dec 7, 2017 at 6:37
  • 1
    Recommend formally answering with that suggestion, @JustinRandall . I's far better than the answers posted so far. Commented Dec 7, 2017 at 6:58

2 Answers 2

4

I was coding up another solution until I checked back and read the comments; they are extremely helpful. Moreover, the suggestion from @JustinRandall was incredibly helpful. I find that find_last_of() and substr() better state the intent of the function--easier to write and easier to read. Thanks! Hope this helps! It helped me.

std::string get_last_word(std::string s) {
  auto index = s.find_last_of(' ');
  std::string last_word = s.substr(++index);
  return last_word;
}

/**
 * Here I have edited the above function IAW 
 * the recommendations.
 * @param s is a const reference to a std::string
 * @return the substring directly
 */
std::string get_last_word(const std::string& s) {
  auto index = s.find_last_of(' ');
  return s.substr(++index);
}
1
  • Fantastic! I'm glad you took the time to provide a concrete example for us. The only suggestion I would make is that when passing in non POD types as input it's good form to pass by reference. If you're not modifying the input it's also good form to make it const. Commented Dec 7, 2017 at 14:54
1

The other answers tell you what's wrong, though you should also know why it's wrong.

In general, you should be very careful about using unsigned value types in loop conditions. Comparing an unsigned type like std::size_t and a signed type, like your constant -1, will cause the signed to get converted into an unsigned type, so -1 becomes the largest possible std::size_t value.

If you put some print statements throughout your code, you'll notice that your loops are never actually entered, because the conditional is always false. Use an int when performing arithmetic and especially when signed numbers are compared with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.