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.
/**
* 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;
}