1044: 统计字母、数字、空格和其它字符的个数(函数
Description
编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
Input
一行字符串
Output
统计数据,4个数字,每个数字后面一个空格。
#include <iostream>
#include <stdio.h>
using namespace std;
void solve(string s){
int alpha,digit,space,other;
alpha=digit=space=other=0;
for(int i=0;i<s.length();i++){
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')){
alpha++;
}else if(s[i]>='0'&&s[i]<='9'){
digit++;
}else if(s[i]==' '){
space++;
}else{
other++;
}
}
cout<<alpha<<" "<<digit<<" "<<space<<" "<<other;
}
int main(){
char s[1000];
gets(s); //从stdin中读取字符串,直至接受到换行符或EOF时停止,并将读取的结果存放到str所的字符串数组中。
solve(s);
return 0;
}