不使用strcmp函数实现比较两个字符串的功能

来源:百度知道 编辑:UC知道 时间:2024/05/11 17:54:50
提示:找第一个不相等字符的代码可用“while(a[i]==b[i]&&a[i]!='\0') i++;”。

不要用指针做 我还没学 希望能帮帮我做 我刚学数组

i = 0;
while((a[i] == b[i]) && (a[i] != '\0')) i++;
if (a[i] == '\0')
printf("The 2 strings are the same.");
else
printf("The 2 strings are different.");
-------------------
大概就是这样吧...

// ret > 0 a >b
// ret = 0 a == b
// ret < 0 a < b
int my_strcmp(const char a[], const char b[])
{
int i = 0;
while(a[i]==b[i]&&a[i]!='\0') i++;
return a[i]-b[i];
}