计算每个角色的出现次数

问题描述

我是C语言的新手,正在从事家庭作业。我完成了大部分工作,但无法通过教授使用的所有测试用例。他拒绝发布自动平地机中正在使用的案例。

我错过了什么情况? 任何线索将不胜感激!!!

Write a program to remove all the blank characters (space and tab) in a line. 
Then,count the number of appearances of each character.

Input
A single line with a maximum length of 5000 characters
Output
First line: The line after removing all the blank characters. 
If the line is empty,don’t print anything in the output.

Next lines: Each line contains a character that appears in the line and its count. 
Note that,the characters must appear according to their ASCII number order. (http://www.asciitable.com)
#include <stdio.h>
int main (){
    int  c = 0;
    int  characters[128] = {0}; //    subscripts for the ASCII table
    int  count           = 0;   //    number of characters been reading in

    while(count < 5001 && (c = getchar()) != EOF) {
        // 9 -> TAB on ASCII,32 -> Space on ASCII
        if (c != 9 && c != 32) {
            putchar(c); 
            characters[c]++;
            count++;
        }
    }
    fflush(stdout);
    printf("\n");
    for (int i = 0; i < 128; i++) {
       if (characters[i] != 0) {
           printf("%c %d\n",i,characters[i]);
        }
    }    
    return 0;
}

再次,我们将不胜感激!

更新: 该代码已更正。

解决方法

可能您不想写

  •  characters[index] = 0;
    

您想要的可能是

  •  text[index] = 0;