Simple C solution

  • 2
    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;


  • 0

    Excuse me,I am a C freshman,not C++. Although I have know C languageand and I can understand some C programs and I can write and run on the Codeblocks. But when I am studying the couse of "data structure" recently, I can't understand the algorithm and I can't write algorithm. I am so worried, please help me. I just have 2 months to understand it deeply.

Log in to reply

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