Problem Description
输入一串字符,统计这串字符里的字母个数,数字个数,空格字数以及其他字符(最多不超过100个字符)
Input
多组测试数据,每行一组
Output
每组输出一行,分别是字母个数,数字个数,空格字数以及其他字符个数
Sample Input
I am a student in class 1.
I think I can!
Sample Output
18 1 6 1
10 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]
}
我的代码:
#include<stdio.h>
int main()
{
char ch[];
while(gets(ch)!=NULL) {
int char_num=,kongge_num=,int_num=,other_num=,i;
for(i=;ch[i]!='\0';i++) {
if(ch[i]>='A' && ch[i]<='Z' || ch[i]<='z' && ch[i]>='a')
{
char_num++;
}
else if(ch[i]==' ')
{
kongge_num++;
}
else if(ch[i]>=''&&ch[i]<='')
{
int_num++;
}
else
{
other_num++;
}
}
printf("%d %d %d %d\n",char_num,int_num,kongge_num,other_num);
}
return ;
}
其他代码:
#include<stdio.h>
#include<string.h>
int main()
{
char str[];//定义字符型数组
int a=, b=, c=, d=;//字母,数字,空格,和其它
while (gets(str) != NULL){
int length = strlen(str);
for (int i = ; i < length; i++){
if (str[i] >= 'A'&&str[i] <= 'Z'||str[i] >= 'a'&&str[i] <= 'z'){
a++;
}
else if (str[i] == ' '){
c++;
}
else if (str[i] >= ''&&str[i] <= ''){
b++;
}
else{
d++;
}
}
printf("%d %d %d %d\n", a, b, c, d);
a=; b=; c=; d=;
}
return ;
}
#include <iostream>
#include<cctype>
using namespace std; char s[];
int main()
{
int alpha,num,space,other;
while(gets(s))
{
alpha=,num=,space=,other=;
for(int i=;s[i]!='\0';++i)
{
if(isalpha(s[i])) ++alpha;
else if(isdigit(s[i])) ++num;
else if(s[i]==) ++space;
else ++other;
}
cout<<alpha<<" "<<num<<" "<<space<<" "<<other<<endl;
}
return ;
}