Simple C solution


  • 1
    C
    int lengthOfLastWord(char* s) {
    int last_len= 0;
    while (*s) {
        while (*s==' ') ++s; // skip spaces
        if (*s) {   
            last_len=0; // new word, reset last word length
            while (*s && *s !=' ') { ++s; ++last_len; }
        }
    }
    return last_len;
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.