我有这个任务来实现 strcmp 函数。有时它运行正常,但有时它会崩溃。请帮我。
#include <iostream>
using namespace std;
int mystrcmp(const char *s1, const char *s2);
int main()
{
cout<<mystrcmp("A","A")<<endl;
cout<<mystrcmp("B","A")<<endl;
cout<<mystrcmp("A","B")<<endl;
cout<<mystrcmp("AB","A")<<endl;
return 0;
}
int mystrcmp(const char *s1, const char *s2)
{
while (*s1==*s2)
{
s1++;
s2++;
}
if(*s1=='\0')
return(0);
return(*s1-*s2);
}