如何使 fgets(或类似语句)的输入字符串 S1 等于我声明的静态字符串 S2?
#include <stdio.h>
#include <string.h>
main() {
char s1[80];
char s2[] = "This is a test.";
int l1, l2;
system("clear");
printf("%s\n\n", s2);
printf("Please type exactly the above sentence: ");
fgets(s1,80,stdin);
l1=strlen(s1);
l2=strlen(s2);
printf("\n String entered for S1, have a length of %d:\n %s", l1, s1);
printf("\n String static for S2, have a legnth of %d:\n %s", l2, s2);
if(s1==s2)
printf("\n\nOk! they're with the same length!\n");
else
printf("\n\nNop! They are NOT the same.\n");
return(0);
}
最佳答案
您正在进行指针比较,而不是字符串比较。要比较字符串的内容,请使用 strcmp
(docs) :
if (strcmp(s1, s2) == 0)
...
关于c - 字符串或不同语句代码中的隐藏或特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25457673/