C //练习 5-4 编写函数strend(s, t)。如果字符串t出现在字符串s的尾部,该函数返回1;否则返回0。
C程序设计语言 (第二版) 练习 5-4
练习 5-4 编写函数strend(s, t)。如果字符串t出现在字符串s的尾部,该函数返回1;否则返回0。
注意:代码在win32控制台运行,在不同的IDE环境下,有部分可能需要变更。
IDE工具:Visual Studio 2010
代码块:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int strend(char *s, char *t){
int len1 = strlen(s);
int len2 = strlen(t);
for(int i = len2 - 1, j = len1 - 1; i >= 0; i--, j--){
if(t[i] != s[j]){
return 0;
}
}
return 1;
}
int main(){
char s[] = "hello";
char t[] = "llo";
printf("%d\n", strend(s, t));
system("pause");
return 0;
}