输入一行字符,分别统计字母,空格,数字和其他字符的个数
#include <stdio.h>
#include <ctype.h>
void Statistics(char *str)
{
int alpha = 0; // 统计字母
int blank = 0; // 统计空格
int num = 0; // 统计数字
int other = 0; //统计其它
for(int i=0;str[i]!='\0';i++) // '\0'是字符串结尾标记
{
if(isalpha(str[i])) //利用库函数判断
{
alpha++;
}
else if(str[i] == ' ')
{
blank++;
}
else if(isdigit(str[i]))
{
num++;
}
else
{
other++;
}
}
printf("字母个数=%d,空格个数=%d,数字个数=%d,其它字符个数=%d\n",alpha,blank,num,other);
}
int main()
{
char *str = "acd *#223 AdS6";
Statistics(str);
return 0;
}