胡作非为我有这样的代码:STRCMP 2个相同的字符串
#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使字符串不相等?可以做任何事情吗?
谢谢
请参阅[strcmp](http://www.cplusplus.com/reference/cstring/strcmp/)部分的返回值。返回的值仅与第一个差异有关(与总长比较无关)。如果要比较字符串长度,请使用[strlen](http://www.cplusplus.com/reference/cstring/strlen/).. – amdixon
是的。 strcat在代码中时返回一个非零数字。当我评论它,然后strcmp返回0 – zeeks
s1 =“abcabc”和s2 =“abc”,比较使得s2比s1短。 –