Skip to content

Commit 240cb25

Browse files
committed
tweak answer
1 parent fa219d9 commit 240cb25

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

chapter05/5-4.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/*
22
* Exercise 5-4. Write the function strend(s, t), which returns 1 if string t
33
* occurs at the end of the string s, and zero otherwise.
4+
*
45
* By Faisal Saadatmand
56
*/
67

@@ -13,13 +14,11 @@ int strend(char *, char *);
1314
/* strend: returns 1 if t occurs at the end of s, and zero otherwise */
1415
int strend(char *s, char *t)
1516
{
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;
17+
s += strlen(s) - strlen(t); /* advance the pointer */
18+
while (*s && *t)
19+
if (*s++ != *t++)
20+
return 0;;
21+
return 1;
2322
}
2423

2524
int main(void)

0 commit comments

Comments
 (0)