华为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;
}
#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;
}
相关文章推荐
- 策略模式
- Java内部类、本地类、匿名类、文件类解读
- 3sum closest to target
- Spark Streaming的容错和数据无丢失机制
- 结对同伴作业复审
- maven 使用maven profile实现多环境可移植构建
- YARN原理及其工作流程
- POJ 1422 Air Raid (最小路径覆盖)
- 浏览器查看Cookie和Http请求头
- c++实现数据结构2.单链表clist
- linux命令-pwd
- OJ 模板-java
- Sharesdk中模拟机不显示QQ空间、微信朋友圈,以及图标下面app名称为英文的解决办法
- SCTP协议及偶联
- SpriteBuilder中的loadAsScene:方法的返回值
- SpriteBuilder中的loadAsScene:方法的返回值
- SpriteBuilder中的loadAsScene:方法的返回值
- Java变量和常量基础
- 优秀的protobuf实例
- [iOS]如何在iOS开发中切换显示语言实现国际化