描述
定义一个一维字符数组string[100],输入一个字符串,含N个字符(N≤100),定义一个整形数组num[5],用于存放统计结果数据,编写函数count()统计字符串中大写字母、小写字母、空格、数字以及其他字符的个数,使用指针完成地址传递,主函数完成数组输入和统计结果输出。
时间限制
1
内存限制
10000
类别
1
输入说明
输入一行字符串,100个以内。
输出说明
格式输出:输出大写字母、小写字母、空格、数字以及其他字符的个数信息,数据之间空一格。
输入样例
A 3cp &! 91 tD M
输出样例
3 3 5 3 2提示
使用指针作形参,实现地址传递,输出数据之间空一格。
1.简版
#include#include #include #include int main() { char string[100]; gets(string); int len = strlen(string); int i; int t1=0, t2=0, t3=0, t4=0, t5=0; for(i=0;i ='A'&&string[i]<='Z') t1++; else if(string[i]>='a'&&string[i]<='z') t2++; else if(string[i]==' ') t3++; else if(string[i]>='0'&&string[i]<='9') t4++; else t5++; } printf("%d %d %d %d %d", t1, t2, t3, t4, t5); return 0; }
2.指针版
#include#include #include #include int main() { char string[100]; gets(string); int len = strlen(string); int i; char *p = string; int t1=0, t2=0, t3=0, t4=0, t5=0; for(i=0,p = string;i ='A'&&*p<='Z') t1++; else if(*p>='a'&&*p<='z') t2++; else if(*p==' ') t3++; else if(*p>='0'&&*p<='9') t4++; else t5++; } printf("%d %d %d %d %d", t1, t2, t3, t4, t5); return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)