[leetcode_58]Length of Last Word

Given a string s consisting of spaces and letters, find the length of the last word. A word is defined as consisting only of letters.

class Solution {
public:
    int lengthOfLastWord(const char *s) {
        int length = strlen(s);
        int index = length - 1;
        while(s[index] == ' ')index--;
        int ans = 0;
        for(int i = index;i >= 0&&s[i] != ' ';i--)
            ans++;
        return ans;
    }
};