Description
编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
Input
一行字符串
Output
统计数据,4个数字,空格分开。
Sample Input
!@#$%^QWERT 1234567
Sample Output
5 7 4 6
编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
Input
一行字符串
Output
统计数据,4个数字,空格分开。
Sample Input
!@#$%^QWERT 1234567
Sample Output
5 7 4 6
#include <iostream> using namespace std; void tongji(char*p,int *q) { (*(q+3))=(*(q+2))=(*(q+1))=*q=0; for(;*p!=' ';p++) { if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')) (*q)++; else if(*p>=48&&*p<=57) (*(q+1))++; else if(*p==' ') (*(q+2))++; else (*(q+3))++; } } int main() { char str[100]; int i,a[4]={0}; cin.getline(str,99); tongji(str,a); for(i=0; i<4; i++) cout<<a[i]<<" "; cout<<endl; return 0; }
#include <iostream> using namespace std; void tongji(char *p,int *aa) { int m=0,n=0,j=0,k=0; for(int i=0;*(p+i)!=' ';i++) { if((*(p+i)>='a'&&*(p+i)<='z')||(*(p+i)>='A'&&*(p+i)<='Z')) { m++; *aa=m; } else if(*(p+i)>='0'&&*(p+i)<='9') { n++; aa[1]=n; } else if(*(p+i)==' ') { j++; aa[2]=j; } else {k++; aa[3]=k; } } } 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; }