博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
武汉科技大学ACM :1003: 零起点学算法67——统计字母数字等个数
阅读量:7240 次
发布时间:2019-06-29

本文共 2562 字,大约阅读时间需要 8 分钟。

Problem Description

输入一串字符,统计这串字符里的字母个数,数字个数,空格字数以及其他字符(最多不超过100个字符)

Input

多组测试数据,每行一组

Output

每组输出一行,分别是字母个数,数字个数,空格字数以及其他字符个数

Sample Input

I am a student in class 1.I think I can!

Sample Output

18 1 6 110 0 3 1

HINT

char str[100];//定义字符型数组

while(gets(str)!=NULL)//多组数据
{
//输入代码
for(i=0;str[i]!='\0';i++)//gets函数自动在str后面添加'\0'作为结束标志
{
//输入代码
}
//字符常量的表示,
'a'表示字符a;
'0'表示字符0;
//字符的赋值
str[i]='a';//表示将字符a赋值给str[i]
str[i]='0';//表示将字符0赋值给str[i]
}

 

我的代码:

1 #include
2 int main() 3 { 4 char ch[100]; 5 while(gets(ch)!=NULL) 6 7 { 8 int char_num=0,kongge_num=0,int_num=0,other_num=0,i; 9 for(i=0;ch[i]!='\0';i++)10 11 {12 if(ch[i]>='A' && ch[i]<='Z' || ch[i]<='z' && ch[i]>='a')13 {14 char_num++;15 }16 else if(ch[i]==' ')17 {18 kongge_num++;19 }20 else if(ch[i]>='0'&&ch[i]<='9')21 {22 int_num++;23 }24 else25 {26 other_num++;27 }28 }29 printf("%d %d %d %d\n",char_num,int_num,kongge_num,other_num);30 }31 return 0;32 }

其他代码:

1 #include
2 #include
3 int main() 4 { 5 char str[100];//定义字符型数组 6 int a=0, b=0, c=0, d=0;//字母,数字,空格,和其它 7 while (gets(str) != NULL){ 8 int length = strlen(str); 9 for (int i = 0; i < length; i++){10 if (str[i] >= 'A'&&str[i] <= 'Z'||str[i] >= 'a'&&str[i] <= 'z'){11 a++;12 }13 else if (str[i] == ' '){14 c++;15 }16 else if (str[i] >= '0'&&str[i] <= '9'){17 b++;18 }19 else{20 d++;21 }22 }23 printf("%d %d %d %d\n", a, b, c, d);24 a=0; b=0; c=0; d=0;25 }26 return 0;27 }
1 #include 
2 #include
3 using namespace std; 4 5 char s[100]; 6 int main() 7 { 8 int alpha,num,space,other; 9 while(gets(s))10 {11 alpha=0,num=0,space=0,other=0;12 for(int i=0;s[i]!='\0';++i)13 {14 if(isalpha(s[i])) ++alpha;15 else if(isdigit(s[i])) ++num;16 else if(s[i]==32) ++space;17 else ++other;18 }19 cout<
<<" "<
<<" "<
<<" "<
<

 

转载于:https://www.cnblogs.com/liuwt365/p/4147404.html

你可能感兴趣的文章
[置顶] STM32移植contiki进阶之三(中):timer 中文版
查看>>
走进C++程序世界-----继承和派生(2)
查看>>
hdoj 2717 Catch That Cow
查看>>
迟来的2013年度总结
查看>>
NoSql数据库使用半年后在设计上面的一些心得 (转)
查看>>
乐在其中设计模式(C#) - 原型模式(Prototype Pattern)
查看>>
纯JavaScripst的全选、全不选、反选 【转】
查看>>
node-webkit学习(3)Native UI API概览
查看>>
基于Windows 配置 nginx 集群 & 反向代理
查看>>
jquery 新建的元素事件绑定问题研究[转]
查看>>
cacti 添加
查看>>
maven中Rhino classes (js.jar) not found - Javascript disabled的处理
查看>>
LTS学习
查看>>
对SIGQUIT的实验 & Java dump
查看>>
软件架构分类(转载)
查看>>
SQL 关于apply的两种形式cross apply 和 outer apply, with cube 、with rollup 和 grouping
查看>>
1028. List Sorting (25)
查看>>
NIO框架之MINA源码解析(四):粘包与断包处理及编码与解码
查看>>
LINUX负载均衡LVS-DR搭建
查看>>
根据xlsx模板生成excel数据文件发送邮件代码
查看>>