从这个网站https://www.programmingsimplified.com/c-program-find-characters-frequency他们有一个例子,它将计算a – z而不是AZ或空格或标准标点符号。
while ( string[c] != ' ' ) { /* 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] != ' ' ) { count[( unsigned char )string[c]]++; c++; } for ( c = 1 ; c < 256 ; c++ ) // no point to check c == 0 { if( count[c] != 0 )
以上就是c/c++开发分享C:读取字符串时,整个ascii表的频率计数(尽可能接近)的可读方式是什么相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/c-cdevelopment/522082.html