编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其他字符的个数,在主函数中输人字符串以及输出上述的结果。
#include<stdio.h>
#include<assert.h>
#include<ctype.h>
void Judge_word(const char* str)
{
int alpha = 0;
int num = 0;
int space = 0;
int other = 0;
while (*str != '\0')
{
if (isalpha(*str)) //判断字符是否是一个字母字符
{
alpha++;
}
else if (isdigit(*str)) //isdigit 判断字符是否是数字字符
{
num++;
}
else if (*str == ' ') //判断字符是否为空格
{
space++;
}
else
{
other++;
}
str++;
}
printf("%d %d %d %d\n", alpha, num, space, other);
}
int main()
{
const char str[] = "zdfh134 ;.;;;dfDFG33";
Judge_word(str);
return 0;
}
输出结果如下: