比较两个字符串大小
虽然知道这是一个很古老的问题,但是还是把我写的拿上来给大家看看,帮我找下错在哪里了。谢谢
int strcmp(char *a,char *b)
{
int number=0;
if (*a != '\0 '&& *b != '\0 ')
{
while(*a== *b)
{
a++;
b++;
number++;
}
if (number == strlen(a))
cout < < "0 " < <endl;
}
else
if(*a == '\0 '&& *b != '\0 ')
cout < < " <0 " < <endl;
if(*a != '\0 '&& *b == '\0 ')
cout < < "> 0 " < <endl;
}
int main()
{
char *source = "abcdefge ";
char *dest = "abcdefeiefe ";
strcmp(source, dest);
}
不知道什么原因,当source <或=dest的时候,总是没有结果输出,> 是输出正确
[解决办法]
int strCmp(const char *s, const char *r)
{
assert(s != NULL && r != NULL);
while(*s == *r && *s != '\0' && *r != '\0')
{
s++;
r++;
}
return (*s-*r);
}