统计字符串种类
时间: 1ms 内存:128M
描述:
用指针编写一个程序,输入字符串后,统计其中各种字符的个数,输出其中大小写字母,数字,以及其他字符的个数。
主函数已经给出,请编写统计字符种类函数。
输入:
一串字符串
输出:
该字符串中大小写字母,数字,以及其他字符的个数,最后输出总字符串长度。
示例输入:
I play LOL for 3 years.
示例输出:
4
12
1
6
23
提示:
参考答案(内存最优[752]):
#include <stdio.h>
int main()
{
char str[100];
gets(str);
char *ptr=str;
void fuction(char *);
fuction(ptr);
return 0;
}void fuction(char *p)
{
int total,cap,sma,num,oth;
total=cap=sma=num=oth=0;
while(*p!=0)
{total++;
if(*p>='A'&&*p<='Z') cap++;
else if(*p>='a'&&*p<='z') sma++;
else if(*p>='0'&&*p<='9') num++;
else oth ++;
p++;
}
printf("%d\n",cap);
printf("%d\n",sma);
printf("%d\n",num);
printf("%d\n",oth);
printf("%d\n",total);
}
参考答案(时间最优[0]):
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{
char str[100];
gets(str);
char *ptr=str;
void fuction(char *);
fuction(ptr);
return 0;
}void fuction(char *p)
{
int total,cap,sma,num,oth;
total=cap=sma=num=oth=0;
while(*p!=0)
{total++;
if(*p>='A'&&*p<='Z') cap++;
else if(*p>='a'&&*p<='z') sma++;
else if(*p>='0'&&*p<='9') num++;
else oth ++;
p++;
}
cout<<cap<<endl;
cout<<sma<<endl;
cout<<num<<endl;
cout<<oth<<endl;
cout<<total<<endl;
}
题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。