Skip to content

Commit

Permalink
Length of last word
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 9, 2017
1 parent 06ed0fb commit 0945258
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 058_length_of_last_word/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test word_length.c
27 changes: 27 additions & 0 deletions 058_length_of_last_word/word_length.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>

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;
}

0 comments on commit 0945258

Please sign in to comment.