-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlast_wrd_len.c
57 lines (43 loc) · 1.04 KB
/
last_wrd_len.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// last_wrd_len: find the length of the last word in a string
// idea taken from https://www.youtube.com/watch?v=5qGMrQKHdkI
// without watching the video
//
// $ gcc -c last_wrd_len.c
// $ gcc -DMAIN -o last_wrd_len last_wrd_len.c
//
// TODO: Test more cases
#ifdef MAIN
#include <stdio.h>
#include <stdlib.h>
#endif
#include <string.h>
#include <ctype.h>
int last_wrd_len(char *s);
#ifdef MAIN
int main(void)
{
// test strings
// char *s = "hello";
//char *s = "test hello";
char *s = "t goodbye";
int l = last_wrd_len(s);
printf("%d\n", l);
return EXIT_SUCCESS;
}
#endif
// find length of last word in a string
int last_wrd_len(char *s)
{
int l, ll;
char *sp;
// get the length of string
ll = l = strlen(s);
if (l < 1) return l;
// set string pointer to end of string, skip '\0'
sp = s+l-1;
// search backwards from end of string for first non-letter
while ((l-- > 1) && isalpha(*sp--))
;
// length of last word is string less current position
return ll - l;
}