解题思路:
解题时一定要看懂题目意思。写两个函数(一个是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语言训练-求s=a+aa+aaa+aaaa+aa...a的值 (C++代码)(手动优化一下计算)浏览:1280 |
回文串 (C语言代码)浏览:2855 |
淘淘的名单 (C语言代码)浏览:1100 |
WU-输入输出格式练习 (C++代码)浏览:1079 |
C语言考试练习题_保留字母 (C语言代码)浏览:700 |
简单的a+b (C语言代码)浏览:478 |
Pascal三角 (C语言代码)浏览:645 |
陶陶摘苹果2 (C语言代码)浏览:597 |
C语言程序设计教程(第三版)课后习题6.7 (C语言代码)浏览:672 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:401 |