sundeepblue
4/27/2014 - 5:55 PM

Length of Last Word

Length of Last Word

int length_of_last_word(const string& s) {
    if(s.empty()) return 0;
    int N = s.size();
    int i = N-1;
    
    while(i >= 0 && s[i] == ' ') i--;
    if(i < 0) return 0;
    int end = i;
    while(i >= 0 && is_letter(s[i])) i--;
    return end - i;
}