解题思路:
解题时一定要看懂题目意思。写两个函数(一个是main函数,另一个是被调函数count),在main函数中输入字符gets(str),注意要将数组空间大小设置在100以上。我们在count函数中去统计从实参传过来的字符串中的各个字符的个数。利用一个for循环,取str的数组长度值strlen(str),并将其赋给n,作为循环的结束条件。
注意事项:
1.注意要调用#include<string.h>和#include<stdlib.h>
2.因为是字符数组,所以字符值要用单引号
参考代码:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int count(char ch[100])
{
int i,n=strlen(ch);
int num=0,charc=0,blank=0,other=0;
for(i=0;i<n;i++) //这里的i是小于n的,而不能等于n,否则会把数组的'\0'这个结束符作为其它符号而other多一
{
if(ch[i]>='0' && ch[i]<='9') num++;
else if(ch[i]>='a' && ch[i]<='z' || ch[i]>='A' && ch[i]<='Z') charc++;
else if(ch[i]==' ') blank++;
else other++;
}
printf("%d %d %d %d",charc,num,blank,other);
return 0;
}
int main()
{
char str[100];
gets(str);
count(str);
return 0;
}
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:528 |
C二级辅导-同因查找 (C语言代码)浏览:624 |
上车人数 (C语言代码)浏览:812 |
WU-格式化数据输出 (C语言代码)浏览:1815 |
C语言程序设计教程(第三版)课后习题6.9 (C语言代码)浏览:745 |
循环入门练习6 (C语言代码)浏览:1038 |
淘淘的名单 (C语言代码)浏览:1299 |
小O的数字 (C语言代码)浏览:1486 |
2003年秋浙江省计算机等级考试二级C 编程题(1) (C语言代码)浏览:850 |
C语言程序设计教程(第三版)课后习题5.6 (C语言代码)浏览:548 |