【发布时间】:2016-03-27 19:10:58
【问题描述】:
所以我正在比较两个字符串,而我的代码正在做与它应该做的完全相反的事情。我无法切换我的 printf 语句,因为那样那也是错误的。如果我输入第一个长度为 5 的字符串和第二个长度为 6 的字符串,它会说字符串 1 比字符串 2 大,而恰恰相反。
这是我所拥有的:
#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 不比较长度:它比较字母排序顺序。
标签: string if-statement strcmp