Description
编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。只要结果,别输出什么提示信息。
Input
一行字符串
Output
统计数据,4个数字,空格分开。
Sample Input
!@#$%^QWERT 1234567
Sample Output
5 7 4 6
HINT
主函数已给定如下,提交时不需要包含下述主函数
/* C++代码 */
int main()
{
char str[100];
int i,a[4];
cin.getline(str,99);
tongji(str,a);
for(i=0; i<4; i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
void tongji(char *str1,int *b);
int main()
{
char str[100];
int i,a[4];
cin.getline(str,99);
tongji(str,a);
for(i=0; i<4; i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
void tongji(char *str1,int *b)
{
int n;
char *p;
n=strlen(str1);
b[0]=b[1]=b[2]=b[3]=0;
for(p=str1;p<str1+n;p++)
{ if(*p>='a'&&*p<='z'||*p>='A'&&*p<='Z')
b[0]++;
else if(*p==' ')
b[2]++;
else if(*p>='0'&&*p<='9')
b[1]++;
else
b[3]++;
}
}