C, 5 lines, single pass, without library calls


  • 0
    M
        cnt = (*s != ' ' && *s != 0) ? 1 : 0;
        for (i = 1; s[i] != 0; ++i)
            if (s[i] != ' ')
                cnt = (s[i - 1] == ' ') ? 1 : cnt + 1;
        return cnt;
    

Log in to reply
 

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