c - 字符串或不同语句代码中的隐藏或特殊字符

标签 c string fgets

如何使 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/

相关文章:

c - 给定 4 个点,其中 2 个在不同的半径上。获取圆心

java - 将各种符号的字符串转换为整数

c - 如何同时接受字符串输入?

javascript - 检测字符串是否包含模式

c - 从没有 '\n' 的输入文件打印行的最佳方法?

c - 为什么这个 fgets() 循环永远不会结束?

c - 我如何在不使用 stdlib.h 的情况下执行此代码

c - for循环中的数字不增加

c - 如何将函数指针作为 const 返回?

c++ - 从字符串流到字符串的转换删除了 '=' 个字符