1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

int LengthOfLastWord(const char *s) 
{
    if(*s == '\0')return 0;

    int count = *s ==' '?0:1;
    while(*(++s) != '\0')
    {
        if (*s != ' ')
            if (*(s-1) == ' ')
                count = 1;
            else
                ++count;
    }

    return count;
}

#include <stdio.h>

int main(int argc, char** argv)
{
    char a[] = "hello world";
    char b[] = "How are you ";

    printf("%d\n", LengthOfLastWord(a));
    printf("%d\n", LengthOfLastWord(b));
    
    return 0;
}
View Program Text


Test Status