测评会员优惠活动进行中 · 开通 VIP,有效期内测评不限次 VIP 优惠中 · 测评不限次 立即查看

A41085. 函数之字符类型统计

填空题 较易

题目描述

函数之字符类型统计

题目描述

编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。

输入格式

一行字符串

输出格式

统计数据,4个数字,空格分开。

样例输入

!@#$%^QWERT    1234567

样例输出

5 7 4 6 

参考答案

#include<stdio.h> #include<string.h> void statistics(char *str, int *num_char, int *num_num, int *space_num, int *other_num) { int len=0, i=0; len = strlen(str); *num_char = 0; *num_num = 0; *space_num = 0; *other_num = 0; for(i = 0; i < len; i++) { if(str[i]>='0' && str[i]<='9') *num_num+=1; else if( (str[i]>='a' && str[i]<= 'z') || (str[i]>='A' && str[i]<='Z')) *num_char+=1; else if(str[i]==' ') *space_num+=1; else *other_num+=1; } } int main() { char Mystring[1024]; int num_char, num_num, space_num, other_num; scanf("%[^\n]", Mystring); statistics(Mystring, &num_char, &num_num, &space_num, &other_num); printf("%d %d %d %d\n", num_char, num_num, space_num, other_num); return 0; }
上一题 下一题