feat: 5-4

This commit is contained in:
Flinner Yuu 2022-07-18 08:27:52 +03:00
parent a8a3cbe224
commit 212ff929fa
Signed by: flinner
GPG Key ID: 95CE0DA7F0E58CA6
1 changed files with 30 additions and 0 deletions

30
chapter-5/5-4.c Normal file
View File

@ -0,0 +1,30 @@
/* 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. */
#include <stdio.h>
int strend(char *ab, char *b) {
// needle does the search
char *needle = b;
// as long as we don't reach the end of ab
while (*ab) {
// check if they match, and move to next char
if (*ab++ != *needle++) {
// reset needle each time they don't match
needle = b;
}
}
// are they both '\0'
return *ab == *needle;
}
int main() {
char *ab = "Hello K&R!";
char *b = "K&R!";
char *c = "Boo!";
printf("Does %s end with %s? %d\n", ab, b, strend(ab, b));
printf("Does %s end with %s? %d\n", ab, c, strend(ab, c));
return 0;
}