【问题标题】:How can I use scanf and if with strings? (Very basic)如何使用 scanf 和 if 字符串? (非常基础)
【发布时间】:2017-04-11 02:34:36
【问题描述】:

我刚刚在大学上过第一堂 C 编程课,我做到了:

#include <stdio.h>
int main()
{
    char answer='x';
    printf("Should the crocodile eat the man? y/n ");
    scanf("%s",&answer);
    printf("%s",& answer);
    if(answer == 'y')
        {printf("the man is dead");}
    if(answer == 'n')
        {printf("the man still alive ");}
}

我怎样才能对字符串而不是单个字符做同样的事情?

我尝试了很多东西,但没有任何效果。

【问题讨论】:

  • 将一个字符串(至少 2 个字符,因为 0 终止符)读入一个字符永远不会工作......
  • 请去问你的导师.. 无论如何答案是你必须使用数组...

标签: c string scanf


【解决方案1】:

在 C 中,字符串 是一系列字符值,后跟一个零值终止符。字符串"hello"由序列{'h', 'e', 'l', 'l', 'o', 0}表示。

字符串存储char的数组 - 数组必须足够大以容纳整个字符串加上 0 终止符,以便存储字符串@987654324 @,您需要一个由char 组成的数组至少 6 个元素。

重要的是要记住char 的数组没有 包含字符串 - 您可以使用它来存储不应被解释为字符串的字符数据。您还可以将多个字符串存储到一个数组中,如下所示:

/**
 * Array size is determined by number of elements in the initializer
 */
char strs[] = {'T','h','i','s',0,'i','s',0,'a',0','t','e','s','t',0}; 

在此之后,strs 将包含 4 个字符串:"This""is""a""test"

因此,要存储 字符串 "y""n",您需要一个 至少 2 个元素宽的 char 数组。

char answer[2];

要使用scanf 读取字符串,请使用%s 转换说明符:

scanf( "%s", answer );

%s 告诉scanf 跳过任何前导空白,然后读取非空白字符的序列 并将它们存储到answer 数组中。 0 终止符将自动附加到数组,使其成为字符串。

在这种情况下,您不需要在answer 上使用&amp; 运算符;在大多数情况下,“T 数组”类型的表达式将被转换(“衰减”)为“指向T 的指针”类型的表达式,因此表达式answer“衰减”从类型char [2]输入char *

为了安全起见,您应该指定一个最大字段宽度,这样您读取的字符就不会超过目标数组的大小:

scanf( "%1s", answer );

由于我们必须为 0 终止符保留一个元素,因此字段宽度说明符应至少比目标数组的大小小 1。由于我们的目标数组的大小可以容纳 2 个元素,因此字段宽度说明符几乎必须为 1。

要比较字符串,请使用strcmpstrncmp 库函数:

if ( strcmp( answer, "y" ) == 0 )
{
  // process "y" answer
}

是的,strcmp 在匹配时返回 0。

您还可以对数组中的单个元素进行比较:

if ( answer[0] == 'y' )
{
  // process "y" answer
}

记住

如果您想从输入流中读取 single 字符并将其存储到带有 scanfsingle 字符对象中,则可以使用 %c 转换说明符:

char answer;             // single character, not an array
scanf( " %c", &answer ); // use & operator to obtain pointer, blank skips
                         // leading whitespace

如果您想从输入流中读取非空白字符的序列,并将它们存储到charscanf数组,您可以使用%s 转换说明符:

char answer[2];          // array of character
scanf( "%1s", answer );  // do not use & operator to obtain pointer, array
                         // expressions are converted to pointer expressions
                         // automatically in this case. 

【讨论】:

  • 你好,约翰——每个人时不时都需要帮助。
  • 本着问题的基本性质,值得一提的是scanf() 会自动将\0 终止符添加到使用%s 转换说明符读取的字符串中。
【解决方案2】:

你需要创建一个缓冲区来存储字符串:

char answer[50]; // change 50 to the maximum length of an answer + 1
scanf("%s", answer);

【讨论】:

    【解决方案3】:

    是的,但您需要为字符串传递一个缓冲区并使用 strcmp 而不是 == 进行比较。

    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
      char answer[16];
      printf("Should the crocodile eat the man? yes/no ");
      scanf("%15s", answer);
      printf("%s", answer);
      if (strncmp(answer, "yes", 16) == 0) {
        printf("the man is dead\n");
      } else if (strncmp(answer, "no", 16) == 0) {
        printf("the man still alive\n");
      }
    }
    

    【讨论】:

    • 调用 strcmp() 函数时不需要 (const char *) 类型转换。使用 strncmp() 而不是 strcmp() 也更安全。
    • scanf("%16s", answer); --> scanf("%15s", answer); 在“宽度”中为 15,扫描和保存的最大字符数。 answer 需要 1 个空字符。
    • strncmp()strnmp() 相比几乎没有优势——如果有的话。如果有的话,检查scanf() 的返回值就像if (scanf("%16s", answer) == 1) ... 更重要,因为它可以让代码知道scanf() 是否成功。
    • 是的。在这种情况下 strncmp() 或 strcmp() 无关紧要。
    猜你喜欢
    • 2012-09-07
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2012-11-26
    • 2017-03-30
    • 1970-01-01
    相关资源
    最近更新 更多