描述
输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数。
数据范围:输入的字符串长度满足 1 \le n \le 1000 \1≤n≤1000
输入描述:
输入一行字符串,可以有空格
输出描述:
统计其中英文字符,空格字符,数字字符,其他字符的个数
示例1
输入:
1qazxsw23 edcvfr45tgbn hy67uj m,ki89ol.\\/;p0-=\\][
复制输出:
26 3 10 12
#include <iostream>
#include <string.h>
#include <algorithm>
#include <string.h>
#include <sstream>
void StatisticsChar(std::string str)
{
int len = strlen(str.c_str());
int englishChar = 0; //英文字符个数
int spaceChar = 0; //空格个数
int numberChar = 0; //数字个数
int othreChar = 0; //其他
for(int i = 0; i < len; ++i)
{
char c = str[i];
if((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
{
englishChar++;
}
else if(c == ' ')
{
spaceChar++;
}
else if(c >= '0' && c <= '9')
{
numberChar++;
}
else
{
othreChar++;
}
}
std::cout << englishChar << std::endl;
std::cout << spaceChar << std::endl;
std::cout << numberChar << std::endl;
std::cout << othreChar << std::endl;
}
int main()
{
std::string str;
getline(std::cin, str);
StatisticsChar(str);
return 0;
}