您的位置:首页 > 其它

华为OJ试题:输入一行字符,分别统计出包含英文字母、空格、数字和其它字符的个数

2015-09-29 19:34 701 查看
#include <stdarg.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <memory>
#include <string.h>
#include <set>
#include <map>
#include <sstream>
#include <string>

using namespace std;

/* 求英文字母的大小*/
static int getEnglishCharCount(string str)
{
int len = str.size();
int i = 0;
int count = 0;

for (i = 0; i < len; i++)
{
if ((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z'))
{
++count;
}
}

return count;
}

/* 求空格字符的个数 */
static int getBlankCharCount(string str)
{
int len = str.size();
int i = 0;
int count = 0;

for (i = 0; i < len; i++)
{
if (str[i] == ' ')
{
++count;
}
}

return count;
}

/* 求数字字符的个数 */
static int getNumberCharCount(string str)
{
int len = str.size();
int i = 0;
int count = 0;

for (i = 0; i < len; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
++count;
}
}

return count;
}

/* 统计出其它字符的个数 */
static int getOtherCharCount(string str)
{
int count = str.size() - getEnglishCharCount(str);
count = count - getBlankCharCount(str);
count = count - getNumberCharCount(str);

return count;
}

int main()
{

string str;

getline(cin, str);

cout << getEnglishCharCount(str) << endl;
cout << getBlankCharCount(str) << endl;
cout << getNumberCharCount(str) << endl;
cout << getOtherCharCount(str) << endl;

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: