(重现strcmp函数)为什么结果不是1?是字符串的输入有问题吗?

#include
#include
#define M 10
/*238-5.编写比较字符串的函数(重现strcmp函数)*/
/* run this program using the console pauser or add your own getch, system("pause") or input loop /
int Strcompare(char *s1,char *s2)
{
char *ss1,*ss2;
ss1=s1;
ss2=s2;
while(
(ss1++)==*(ss2++)&&*(ss1++)!='0'&&*(ss2++)!='0')
{

}

return *s1-*s2 ;

}
int main(int argc, char *argv[])
{
char *s1,*s2,str1[M],str2[M];
int i=0,j=0;
int a;
while(((str1[i++]=getchar())!='\n')&&((str2[j++]=getchar())!='\n'));
s1=str1;
s2=str2;
a=Strcompare(s1,s2);
printf("%d",a);

return 0;

}
图片说明


我调试了下,你的输入就不对,看下面的程序

#include <stdio.h> 
#include <stdlib.h> 
#define M 10
/*238-5.编写比较字符串的函数(重现strcmp函数)*/

int Strcompare(char *s1,char *s2)
{
    while (1)
    {
        if (*s1 == 0 && *s2 == 0) return 0;
        if (*s1 == 0) return - 1;
        if (*s2 == 0) return 1;
        if (*s1 != *s2) return *s1 - *s2;
        s1++;
        s2++;
    }
    return 0;
}
int main(int argc, char *argv[])
{
    char *s1,*s2,str1[M],str2[M];
    int i=0,j=0;
    int a;
    //while(((str1[i++]=getchar())!='\n')&&((str2[j++]=getchar())!='\n'));
    scanf("%[^\n]", str1);
    fflush(stdin);
    scanf("%[^\n]", str2);
    s1=str1;
    s2=str2;
    a=Strcompare(s1,s2);
    printf("%d",a);
    return 0;
}

用换行来输入
以下是程序运行:
a
b
-1Press any key to continue . . .

#include
#include
#define M 10
/*238-5.编写比较字符串的函数(重现strcmp函数)*/
/* run this program using the console pauser or add your own getch, system("pause") or input loop /
int Strcompare(char *s1,char *s2)
{
char *ss1,*ss2;
ss1=s1;
ss2=s2;
while((ss1++)==
(ss2++)&&*(ss1++)!='0'&&*(ss2++)!='0')
{

}

return *s1-*s2 ;
}
int main(int argc, char *argv[])
{
char *s1,*s2,str1[M],str2[M];
int i=0,j=0;
int a;
//while(((str1[i++]=getchar())!='\n')&&((str2[j++]=getchar())!='\n'));
strcpy(str1,"a");
strcpy(str2,"b");
s1=str1;
s2=str2;
a=Strcompare(s1,s2);
printf("%d",a);

return 0;
}
while()字符串获取不对 其他没问题 使用getchar()搭配while循环获取字符串这个想法没问题,但是存在缓存问题,使输入字符串可打印字符串不符合,如图:
图片说明

以下是正确执行结果

![图片说明](https://img-ask.csdn.net/upload/201912/03/1575373290_573479.png)