【问题标题】:How to read a user input with multiple characters through scanf without using strings如何在不使用字符串的情况下通过scanf读取具有多个字符的用户输入
【发布时间】:2019-08-31 10:05:35
【问题描述】:

我正在为一门课程开发一个程序,该程序需要读取用户输入的单行并计算该行中每个字母的数量。每个字母的数量放在一个数组中。例如,如果用户输入“Apple”,程序应将 1 a、2 ps、1 l 和 1 e 放入计数数组中。我的问题是我的教授说我不能在程序中使用字符串。我不确定如何存储多个字符,以便 scanf 可以在不使用字符串的情况下读取它们。在寻求帮助时,他说一次读取输入的一个字符。

我尝试在 do-while 循环中使用 scanf 来读取输入的每个字符,但我的大多数尝试都以只读取第一个字符并放入记录字母数量的数组而告终。

printf("ENTER A LINE OF TEXT: \n");
  do {
  scanf("%c ", &userChar);      
  userChar = toupper(userChar);
  userCharVal = userChar;
  histo[userCharVal - 65] = histo[userCharVal - 65] + 1;
  } while(userChar == '\n');

如果用户输入“apple”,相应的数组 histo[] 将根据输入的字母进行更新。一世。 e.如果用户输入的是apple,程序会先将histo[0]加1(对应'a'),然后读取该单词的下一个字符。它应该在换行符处结束读取用户输入。实际上,程序只是记录第一个字符,然后结束。

【问题讨论】:

  • 使用fgetc 而不是fscanf
  • 您可能想要while(userChar != '\n') 而不是==

标签: c


【解决方案1】:

您的代码有两个问题。

首先,scanf("%c ", &userChar); 将读取单个字符,然后跳过后面的任意数量的空白字符。 (请参阅this answer 了解更多信息)空白字符通常表示''、'\t'、'\r'、'\n'、'\v'、'\f'。因此,您的 userChar永远在您的代码中包含换行符。

其次,你的 do-while 条件不正确。你让它运行“而 userChar 等于换行符”,这总是错误的,所以循环永远不会运行超过一次。你想要的条件是userChar != '\n'

所以,试试以下方法:

    printf("ENTER A LINE OF TEXT: \n");
    do
    {
        scanf("%c ", &userChar);
        userChar = toupper(userChar);
        userCharVal = userChar;
        histo[userCharVal - 65] = histo[userCharVal - 65] + 1;
    } while (userChar != '\n');

【讨论】:

    【解决方案2】:

    你的问题是

    while(userChar == '\n');
    

    当您在"Apple" 中读取'A' 时,userChar 不会== '\n' 导致您的 oop 在第一次迭代后终止。你的意图是:

    while(userChar != '\n');
    

    注意"!="而不是"=="

    这样,您的代码应该可以工作,尽管没有必要使用单独的 userCharuserCharVal

    要稍微清理一下,只需使用单个int 来读取带有getchar() 的字符,不需要scanf(最好避免使用)。需要的额外验证是该字符是[a-zA-Z],以确保您使用histo[userCharVal - 65] 递增有效索引。 (当你可以使用'A' 清楚地表明你在做什么时,避免使用像65 这样的数字)

    总而言之,你可以做类似的事情:

    #include <stdio.h>
    #include <ctype.h>
    
    #define NCHR   26   /* to cover each character in the alphabet */
    
    int main (void) {
    
        int frequency[NCHR] = {0},  /* frequency of each char initialized to zero */
            c;                      /* character returned by getchar() */
    
        fputs ("enter a line of text: ", stdout);
        while ((c = getchar()) != '\n' && c != EOF) /* read each char */
            if (isalpha(c))                         /* validate a-zA-Z */
                frequency[toupper(c) - 'A']++;      /* increment element */
    
        puts ("\nfrequency of characters:\n");
        for (c = 0; c < NCHR; c++)
            if (frequency[c])   /* output lowercase per your example */
                printf (" %c : %d\n", c + 'a', frequency[c]);
    }
    

    (注意:还需要对EOF 进行额外检查。不能保证'\n' 用户可以使用Ctrl+dEOF /kbd> 在 Linux 上,或 Ctrl+z 在 Windows 上,表示输入结束,或完全取消输入)

    使用/输出示例

    $ ./bin/charfreqarray
    enter a line of text: Apple
    
    frequency of characters:
    
     a : 1
     e : 1
     l : 1
     p : 2
    

    检查一下,如果您还有其他问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-31
      相关资源
      最近更新 更多