How would I get the length of the last word in a string of input?

Write your question here.
I know how to get the length of one word or the length of all words in a single input string. But I have to get the last word which can be separated by any number of spaces. It is the spaces that confuse me because the input could be " Hello World " with all those spaces and I'm supposed to identify the last word "world" and how many characters are in it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/**
 * MEMBER FUNCTION NAME: 
 *    countLastWordLength(const string str)
 * PURPOSE: 
 *    The function takes in a constant C++ input string, consisting of 0 to many    *      words    and     spaces    and 
 *    returns the length of the last word in the input string
 * PARAMETER:
 *    const string: str
 * RETURN VALUE:
 *    int: the length of the last word in the input string
 */

#include <iostream>
  
class CIS14 {
public:
   int countLastWordLength(const string str);
};

int main()
{
    // create an instance of the CIS14 class 
    CIS14 cis14; 
    
    // invoke the cis14 object's public member function called      countLastWordLength(), 
    // and print its output to screen
    cout << cis14.countLastWordLength("Hello World") << endl;

    // exiting program
    return 0;
}
Try world.length
Topic archived. No new replies allowed.