Created
November 26, 2009 07:38
-
-
Save shaobin0604/243308 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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> | |
| #include <string.h> | |
| int strend(char *s, char *t) | |
| { | |
| int slen = strlen(s); | |
| int tlen = strlen(t); | |
| int i, j; | |
| for (i = slen - 1, j = tlen - 1; i >= 0 && j >=0 && *(s + i) == *(t + j); i--, j--) | |
| ; | |
| if (j < 0) | |
| return 1; | |
| else | |
| return 0; | |
| } | |
| int main(void) | |
| { | |
| char *s1 = "some really long string."; | |
| char *s2 = "ng."; | |
| char *s3 = "ng"; | |
| if(strend(s1, s2)) | |
| { | |
| printf("The string (%s) has (%s) at the end.\n", s1, s2); | |
| } | |
| else | |
| { | |
| printf("The string (%s) doesn't have (%s) at the end.\n", s1, s2); | |
| } | |
| if(strend(s1, s3)) | |
| { | |
| printf("The string (%s) has (%s) at the end.\n", s1, s3); | |
| } | |
| else | |
| { | |
| printf("The string (%s) doesn't have (%s) at the end.\n", s1, s3); | |
| } | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment