6-1 利用指针,实现字符串比较函数 (30分)
编写字符串比较函数,strmycmp(s,t),功能:比较字符串s,t的大小,返回比较结果。函数的返回值为两个串从左至右第一个不同字符相差的acsii码的值。如果都相同返回0。
函数接口定义:
在这里描述函数接口
例如: int strmycat(char *s,char *t)
在这里解释接口参数。例如:其中 s 和 t 都是用户传入的参数。 s 的第一个字符串; t 是第二个字符串。函数的返回值为整数值。输入的字符串不超过100个字符。
裁判测试程序样例:
在这里给出函数被调用进行测试的例子。例如:
#include <stdio.h>
int strmycat(char *s,char *t);
int main()
{
char s1[201],s2[101];
gets(s1);
gets(s2);
printf("%d",strmycat(s1,s2));
return 0;
}
/* 请在这里填写答案 */
输入样例:
在这里给出一组输入。例如:
li
maming
输出样例:
在这里给出相应的输出。例如:
-1
#include<string.h>
int strmycat(char *s,char *t)
{
int len1 = strlen(s),len2 = strlen(t);
int max = len1,x=0;
if(len2<len1)max = len2;
for(int i=0;i<max;i++)
{
if(s[i] != t[i])
{
x = s[i] - t[i];
break;
}
}
return x;
}
int strmycat(char *s,char *t)
{
while(*s && *t&& *s == *t){
s++;
t++;
}
return (*s-*t);
}