We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent fa219d9 commit 240cb25Copy full SHA for 240cb25
chapter05/5-4.c
@@ -1,6 +1,7 @@
1
/*
2
* Exercise 5-4. Write the function strend(s, t), which returns 1 if string t
3
* occurs at the end of the string s, and zero otherwise.
4
+ *
5
* By Faisal Saadatmand
6
*/
7
@@ -13,13 +14,11 @@ int strend(char *, char *);
13
14
/* strend: returns 1 if t occurs at the end of s, and zero otherwise */
15
int strend(char *s, char *t)
16
{
- unsigned slen, tlen;
17
-
18
- if ((slen = strlen(s)) >= (tlen = strlen(t))) /* check s's boundaries */
19
- for (s += slen - tlen; *s == *t; s++, t++)
20
- if (*s == '\0')
21
- return 1; /* ends match */
22
- return 0;
+ s += strlen(s) - strlen(t); /* advance the pointer */
+ while (*s && *t)
+ if (*s++ != *t++)
+ return 0;;
+ return 1;
23
}
24
25
int main(void)
0 commit comments