C:读取字符串时,整个ascii表的频率计数(尽可能接近)的可读方式是什么

从这个网站http://www.programmingsimplified.com/c-program-find-characters-frequency他们有一个例子,它将计算a – z而不是AZ或空格或标准标点符号。

while ( string[c] != '\0' ) { /* Considering characters from 'a' to 'z' only */ if ( string[c] >= 'a' && string[c] <= 'z' ) count[string[c]-'a']++; c++; } for ( c = 0 ; c < 26 ; c++ ) { if( count[c] != 0 ) 

要计算字符串中的所有字符,请使用int a[256]并使用字符串的字符作为数组的索引并增加:

 int counts[256] = { 0 }; /* Initialize all elements to zero. */ while (string[c]) counts[(unsigned char)string[c++]]++; 

我不确定我理解你的问题,但我担心在提出问题之前我没有尝试解决它。

 int count[ 256 ] ; // - make sure you change your declaration from [ 26 ]! while ( string[c] != '\0' ) { count[( unsigned char )string[c]]++; c++; } for ( c = 1 ; c < 256 ; c++ ) // no point to check c == 0 { if( count[c] != 0 )