c - 标准输入上最受欢迎的字符

标签 c ascii

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
    int ascii[255]; //starts as empty table, will hold all the character occurences
    memset(ascii, 0, sizeof(ascii)); // sets all table values to 0
    int c=0;
    int i=0;
    while (getchar() !=EOF){
        c=getchar();
        ascii[c]=(ascii[c]+1);
    }
    for (i=0;i<255;i++){
        printf("%d;",ascii[i]);
    }
    return 0;
}

您好,我创建了上面的代码来检查每个字符在 .txt 文件中出现的次数,但是我的行为非常不稳定,我得到的数字根本不反射(reflect)文件的内容。你能告诉我我的错误在哪里吗?

最佳答案

您有两个 getchar() 调用,因此每次调用中缺少一个字符,请更改此

while (getchar() != EOF)

while ((c = getchar()) != EOF)

并删除下一行

c = getchar();

关于c - 标准输入上最受欢迎的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28004799/

相关文章:

c - 如何在labwindows CVI中将文本打印到列表框

c - 如何将字符保存到数组中其ascii数字的位置在c中

javascript - 哪些字符与 jquery keydown 事件一起使用。which

c - C 编译器是否真的有一个 ascii 查找表

java - 替换java中除撇号之外的非字母

c - 在创建霍夫曼树时哪个节点在增加权重时向左或向右

c - 将数字分开然后求和的函数

python - 不完整的 gamma 函数 : can this code get any faster in cython, C 或 Fortran?

c++ - 自定义 3D(并行)FFTW 转换可能吗?

c++ - cin 如何将字符转换为相应的类型