【发布时间】:2015-09-28 18:24:34
【问题描述】:
我有这个代码:
#include <stdio.h>
#include <string.h>
int main() {
char s1[50], s2[50];
printf("write s1:\n");
fgets(s1, sizeof(s1), stdin);
printf("s2:\n");
fgets(s2, sizeof(s2), stdin);
printf("The concatenation of the two strings: %s\n", strcat(s1, s2));
if( strcmp(s2, s1) < 0 ) {
printf("s2 is shorter than s1.\n");
} else if( strcmp(s2, s1) > 0 ) {
printf("s2 is longer than s1.\n");
} else {
printf("strings are equal.\n");
}
return 0;
}
问题是,当我写 2 个相同的字符串(如 abc 或其他)时,strcmp 返回“s2 比 s1 短。”
这是正常输出还是我做错了什么?如果有,在哪里?
或者 strcat 使字符串不相等?对此有什么办法吗?
谢谢
【问题讨论】:
-
是的。当 strcat 在代码中时,strcmp 返回一个非零数。当我评论它时,strcmp 返回 0
-
s1 = "abcabc" and s2 = "abc",比较使s2比s1短。
-
所以代码没问题,这应该是正确的答案?