-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstrlonger.c
More file actions
53 lines (44 loc) · 1.41 KB
/
strlonger.c
File metadata and controls
53 lines (44 loc) · 1.41 KB
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* $begin strlen */
/* Prototype for library function strlen */
size_t strlen(const char *s);
/* $end strlen */
/* $begin strlonger */
/* Determine whether string s is longer than string t */
/* WARNING: This function is buggy */
int strlonger(char *s, char *t) {
return strlen(s) - strlen(t) > 0;
}
/* $end strlonger */
/* Determine whether string s is longer than string t */
int strlonger2(char *s, char *t) {
/* $begin strlonger-fix */
return strlen(s) > strlen(t);
/* $end strlonger-fix */
}
/* $begin strshorter */
/* Determine whether string s is shorter than string t */
/* WARNING: This function is buggy */
int strshorter(char *s, char *t) {
return strlen(s) - strlen(t) < 0;
}
/* $end strshorter */
/* Determine whether string s is shorter than string t */
int strshorter2(char *s, char *t) {
/* $begin strshorter-fix */
return strlen(s) < strlen(t);
/* $end strshorter-fix */
}
int main(int argc, char *argv[]) {
char *s1 = argc > 1 ? argv[1] : (char *) "howdy";
char *s2 = argc > 2 ? argv[2] : (char *) "doddy";
int l1 = strlonger(s1, s2);
int l2 = strlonger2(s1, s2);
int r1 = strshorter(s1, s2);
int r2 = strshorter2(s1, s2);
printf("'%s':'%s'. Longer 1? : %d. Longer 2? : %d\n", s1, s2, l1, l2);
printf("'%s':'%s'. Shorter 1? : %d. Shorter 2? : %d\n", s1, s2, r1, r2);
return 0;
}