1416 - C语言-字符统计
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
Input
一行字符
Output
统计值
Examples
Input
aklsjflj123 sadf918u324 asdf91u32oasdf/.';123
Output
23 16 2 4
Solution C
int main(int argc, char* argv[]) { int a,b,c,d,i; char str[100]; while(gets(str)) { a=b=c=d=0; for(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; }
Solution C++
#include<iostream> #include<fstream> #include<string> #include<cstring> #include<cmath> using namespace std; int main() { //ifstream cin("aaa.txt"); int i,j,n,m,x1,x2,x3,x4,len; x1=x2=x3=x4=0; string s; getline(cin,s); len=s.size(); for(i=0;i<len;i++) { if(isalpha(s[i])) x1++; else if(s[i]==' ') x2++; else if(isdigit(s[i])) x3++; else x4++; } cout<<x1<<" "<<x3<<" "<<x2<<" "<<x4<<endl; return 0; }