#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/