stdin上最受欢迎的角色 #包括 #包括 #包括 int main() { int ascii[255];//以空表开始,将保存所有出现的字符 memset(ascii,0,sizeof(ascii));//将所有表值设置为0 int c=0; int i=0; while(getchar()!=EOF){ c=getchar(); ascii[c]=(ascii[c]+1); } 对于(i=0;i

stdin上最受欢迎的角色 #包括 #包括 #包括 int main() { int ascii[255];//以空表开始,将保存所有出现的字符 memset(ascii,0,sizeof(ascii));//将所有表值设置为0 int c=0; int i=0; while(getchar()!=EOF){ c=getchar(); ascii[c]=(ascii[c]+1); } 对于(i=0;i,c,ascii,C,Ascii,您有两个getchar()调用,因此每次调用中缺少一个字符,请更改此设置 #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)); //

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

#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;
}

然后删除下一行

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

调试起来没什么大不了的……起初我做的是ascii[c]++,但我不确定它是否合法(我对这个很陌生),所以我决定用安全的方式进行调试now@KONAKONA这对行:'while(getchar()!=EOF){c=getchar();'skip每隔一个输入字符!suggest'而((c=getchar())!=EOF)这应该是
int ascii[256]
…啊,我明白了,这是因为getchar从输入中删除了一个读取字符,对吗?@KONAKONA是的,你完全放弃了第一个字符。谢谢,现在它就像一个符咒。顺便问一下,你知道如果我在我的文件中放入一个非ascii字符会发生什么吗?只要阅读手册,它会将一个
无符号字符
转换为
int
@KONAKONA:
getchar
读取单字节字符,不解释它们。它还返回一个介于0和255之间的值或
EOF
,因此您的程序是安全的。结果取决于您的编码。
while ((c = getchar()) != EOF)
c = getchar();