Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 0945258

Browse files
Length of last word
Signed-off-by: Leo Ma <begeekmyfriend@gmail.com>
1 parent 06ed0fb commit 0945258

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

058_length_of_last_word/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
gcc -O2 -o test word_length.c

058_length_of_last_word/word_length.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int lengthOfLastWord(char *s)
5+
{
6+
int len = 0;
7+
while (*s != '\0') {
8+
if (s[-1] == ' ' && s[0] != ' ') {
9+
len = 1;
10+
} else if (*s != ' ') {
11+
len++;
12+
}
13+
s++;
14+
}
15+
return len;
16+
}
17+
18+
int main(int argc, char **argv)
19+
{
20+
if (argc != 2) {
21+
fprintf(stderr, "Usage: ./test word\n");
22+
exit(-1);
23+
}
24+
25+
printf("%d\n", lengthOfLastWord(argv[1]));
26+
return 0;
27+
}

0 commit comments

Comments
 (0)