您的位置:首页 > 其它

7.9编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输入字符串以及输出上述的结果。

2019-01-24 10:31 781 查看
//C程序设计第四版(谭浩强)
//章节:第七章 用函数实现模块化程序设计
//题号:7.9
//题目:编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,
//在主函数中输入字符串以及输出上述的结果。
#include <stdio.h>
#include <string.h>
void count(char s[])
{
int letter=0,number=0,space=0,other=0,i,len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]>='a'&&s[i]<='z'||s[i]>='A'&&s[i]<='Z')
letter++;
else if(s[i]>='0'&&s[i]<='9')
number++;
else if(s[i]==' ')
space++;
else
other++;
}
printf("letter:%d\nnumber:%d\nspace:%d\nother:%d\n",letter,number,space,other);
}
int main()
{
char s[80];
printf("input string:\n");
gets(s);
count(s);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐