所以我比较了两个字符串,我的代码和它应该做的事情完全相反。我无法切换我的printf语句,因为那样也会出错。如果我输入长度为5的第一个字符串和长度为6的第二个字符串,它会说当字符串1完全相反时,字符串1比字符串2大。
以下是我有:如果我的C代码中的语句与条件完全相反?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char str1[100], str2[100];
printf("enter the first string :");
scanf("%s", &str1);
printf("enter the second string :");
scanf("%s", &str2);
if(strcmp(str1,str2)==0){
printf("the strings are equal\n");
}
else if(strcmp(str1,str2)<0){
printf("string 1 is less than string 2\n");
}
else{
printf("string 1 is greater than string 2\n");
}
return 0;
}
Strcmp不比较长度:它比较字母排序顺序。 – slim