C++字符统计

来源:互联网 发布:手机片头制作软件 编辑:程序博客网 时间:2024/06/03 22:54
#include <iostream>#include <string>using namespace std;int main(){string str = "";       //char str[200];cin >> str;       //cin.getline(str,200);int cnt[256]={};for(int i = 0; i < str.size(); i++)
//for(int i =0; i < strlen(str); i++)
{cnt[(int)str[i]]++;}for(int i = 0; i < 256; i++){ if(cnt[i] != 0){cout << (char)i << ":" << cnt[i]<< endl;}}return 0;}

PS:使用注释部分可以读取空格和标点符号字符。

原创粉丝点击