c语言:写一个函数统计字符串的字母,数字,空格,其他字符
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<ctype.h>
#pragma warning(disable:4996)
#define n 1024
void fun(char str[]);
int main(){
char str[n];
printf("请输入字符串:\n");
gets(str);
fun(str);
return 0;
}
void fun(char str[]){
int num,letter,space,other;
num=letter=space=other=0;
for ( int i = 0; i < strlen(str); i++)
{
if (isalpha(str[i]))
{
letter++;
}else if (str[i]==' ')
{
space++;
}else if (isdigit(str[i]))
{
num++;
}else{
other++;
}
}
printf("空格%d\n",space);
printf("字母%d\n",letter);
printf("数字%d\n",num);
printf("其他字符%d\n",other);
}