//*作者:冰糖
//*日期:2018年11月20日
//*章节:第七章
//*题号: 9
//*题目:编写一个函数,由实参传来一个字符串,统计此字符串中字母,数字,空格和其他字符的个数,在主函数中输入字符串并输出以上结果。
#include<stdio.h>
int x[4];
int main()
{
void qaq(char h[]);
char ch[100];
int j;
gets(ch);
printf("此字符串中字母,数字,空格和其他字符的个数分别为:\n");
qaq(ch);
for(j=0;j<4;j++)
{
printf("%d ",x[j]);
}
return 0;
}
void qaq(char h[])
{
int a=0,b=0,c=0,d=0,len,i;
for(i=0;h[i]!='\0';i++)
{
if(('a'<=h[i]&&h[i]<='z')||('A'<=h[i]&&h[i]<='Z'))
{
a++;
}
else if(('0'<=h[i])&&(h[i]<='9'))
{
b++;
}
else if(h[i]==' ')
{
c++;
}
else
{
d++;
}
}
x[0]=a;
x[1]=b;
x[2]=c;
x[3]=d;
}
C语言程序设计第四版(谭浩强)7.9 编写一个函数,由实参传来一个字符串,统计此字符串中字母,数字,空格和其他字符的个数,在主函数中输入字符串并输出以上结果。
最新推荐文章于 2023-04-06 15:34:44 发布