如何使我的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);
}
Run Code Online (Sandbox Code Playgroud)
你正在进行指针比较,而不是字符串比较.要比较字符串的内容,请使用strcmp(docs):
if (strcmp(s1, s2) == 0)
...
Run Code Online (Sandbox Code Playgroud)