clueless coding // TODO: be smarter

LeetCode Length of Last Word: CPP Solution



TL;DR Code Solution


int lengthOfLastWord(string s) {

    int right = s.length() - 1;

    while (right >= 0 && s[right] == ' ')
        right--;

    int count = 0;
    while (right >= 0 && s[right] != ' ') {
        count++;
        right--;
    }

    return count;

}