问题:

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.
If the last word does not exist, return 0.

解答:

(这个问题不难,但是比较容易错,特别是有的情况考虑不周全)

int lengthOfLastWord(const char *s)
{
int len = strlen(s);
int sum = 0;
while(s[len - 1] == ' ') len--;
for(int i = len - 1; i >= 0; i--)
{
if(s[i] != ' ') sum++;
else break;
}
return sum;
}