2091 - 字符串统计
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
题目输入
一行字符
题目输出
统计值
输入/输出样例
题目输入
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
题目输出
23 16 2 4
C语言解答
#include<stdio.h> #include<string.h> main() { char ch[10000]; int i,num1,num2,num3,num4; while(gets(ch)!=NULL) { for(i=0,num1=0,num2=0,num3=0,num4=0;i<strlen(ch);i++) { if(ch[i]>='A'&&ch[i]<='Z'||ch[i]>='a'&&ch[i]<='z') num1++;//character else if(ch[i]==' ') num2++;//space else if(ch[i]>='0'&&ch[i]<='9') num3++;//number else num4++;//else } printf("%d %d %d %d\n",num1,num3,num2,num4); } }
C++解答
#include<iostream> #include<string> #include<cctype> using namespace std; int main() { string s; getline(cin,s); int letter=0,space=0,digit=0,other=0; int t=s.size(); for (int i=0; i<t; i++) if (isalpha(s[i])) letter++; else if (isdigit(s[i])) digit++; else if (s[i]==' ') space++; else other++; cout<<letter<<" "<<digit<<" "<<space<<" "<<other<<endl; return 0; }