diff --git a/058_length_of_last_word/Makefile b/058_length_of_last_word/Makefile new file mode 100644 index 0000000..fe49695 --- /dev/null +++ b/058_length_of_last_word/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test word_length.c diff --git a/058_length_of_last_word/word_length.c b/058_length_of_last_word/word_length.c new file mode 100644 index 0000000..b18cec9 --- /dev/null +++ b/058_length_of_last_word/word_length.c @@ -0,0 +1,27 @@ +#include +#include + +int lengthOfLastWord(char *s) +{ + int len = 0; + while (*s != '\0') { + if (s[-1] == ' ' && s[0] != ' ') { + len = 1; + } else if (*s != ' ') { + len++; + } + s++; + } + return len; +} + +int main(int argc, char **argv) +{ + if (argc != 2) { + fprintf(stderr, "Usage: ./test word\n"); + exit(-1); + } + + printf("%d\n", lengthOfLastWord(argv[1])); + return 0; +}