C++的!!输入一行字符,分别求出其中字母,数字,空格,其他字符的个数。最好能用循环做!其他的也行!

来源:百度知道 编辑:UC知道 时间:2024/09/21 15:23:40

#include <map>
#include <iostream>
#include <algorithm>
#include <string>
#include <iomanip>
using namespace std;

int main()
{
typedef unsigned int size_t;
typedef map<char, size_t> mapc_t;
mapc_t store;
cout << "Please enter a line of words: " << endl;
string line;
getline(cin, line);

for(string::iterator ite = line.begin(); ite != line.end(); ++ite)
{
if(!(store.insert(mapc_t::value_type(*ite, 1))).second)
++store[*ite];
}

for(mapc_t::iterator ite = store.begin(); ite != store.end(); ++ite)
{
cout << ite->first << " : " << setw(3) << ite->second << " times" << endl;
}
}

谢谢楼下的提醒,上面是我没怎么注意看就随便写的,我以为楼主要统计各个字符的出现次数,但是却是统计各类型的字符的出现次数,这样的话在遍历循环中用标准库cctype的那几个函数进行下测试然后再统计下就可以了:

#include <iostrea

C语言 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。 C问题,用FOR语句做:输入一行字符,分别统计其中英文字母,空格,数字和其他字符的个数. 求解:用C.输入一行字符,分别统计出其中英文字母,空格,数字和其它字符的个数 用C语言编程:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数. C语言题:输入一行字符,分别统计出其中的英文字母、空格、数字和其它字符。 输入一行字符,分别统计其中的英文大写字母,小写字母,数字字符和其他字符 c中输入一行字符,将最长的输出 输入一行字符,分别统计出其中的英文字母.空格.数字.和其他字符的个数! 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。 用java能做出来么?C的我不要!谢谢 题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。