blob: 8fec2a2a1ad0249d63e2ea7265964ec4f62fabab (
plain)
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
|
#include <stdio.h>
#include <string.h>
/* The C Programming Language: 2nd Edition
*
* Exercise 5-4: Write the function strend(s,t), which returns 1 if the string
* t occurs at the end of the string s, and zero otherwise.
*
* Answer: Check from the end of the string!
*/
int strend(char *, char *);
int main() {
char *foo = "kekt";
char *bar = "buckets of kekt";
printf("%d\n", strend(bar, foo)); // should output 1
foo = "missile";
bar = "Did you see those missiles?!";
printf("%d\n", strend(bar, foo)); // should output 0
return 0;
}
int strend(char *s, char *t) {
// Go to the end of s
while (*s != '\0') {
s++;
}
// back up s by however long t is
s -= strlen(t);
// Check to be sure the rest of the string matches
for (; *s == *t && *s != '\0' && *t != '\0'; s++, t++);
if (*s == '\0' && *t == '\0') {
return 1;
}
return 0;
}
|