从这个网站 http://www.programmingsimplified.com/c-program-find-characters-frequency 他们有一个示例可以计算 a - z 但不计算 A-Z 或空格或标准标点符号。
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++]]++;
关于C:什么是在读取字符串时对整个 ascii 表进行频率计数(尽可能接近)的可读方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14904674/