Problem B: 零起点学算法101——统计字母数字等个数
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
#include<stdio.h>
#include<string.h>
int main(){
char str[100];
while(gets(str)!=NULL){
int a=0,b=0,c=0,d=0;
for(int i=0;str[i]!='\0';i++){
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')){
a++;
}
else if(str[i]>='0'&&str[i]<='9'){
b++;
}
else if(str[i]==' '){
c++;
}
else{
d++;
}
}
printf("%d %d %d %d\n",a,b,c,d);
}
return 0;
}
本文介绍了一个简单的字符统计算法,通过输入一串字符,能够统计出这串字符中的字母、数字、空格及其他字符的数量。算法使用C语言实现,适合初学者理解算法的基本概念。
809

被折叠的 条评论
为什么被折叠?



