//统计一个字符串中字母,数字,空格的个数
#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;
int main()
{
char *str = (char *)malloc(50*sizeof(char));
printf("please input some characters:");
gets(str);
int alpha=0,num=0,upper=0,lower=0,space=0;
char *s = str;
int tag = 0;
for(int i=0;i<strlen(str);i++)
{
if(isalpha((int)(tag=*(s+i))))
{
alpha++;
if(isupper(tag))
{
upper++;
}
else
{
lower++;
}
}
else if(isdigit(tag))
{
num++;
}
else if(isspace(tag))
{
space++;
}
}
printf("alpha is %d\n",alpha);
printf("upper is %d\n",upper);
printf("lower is %d\n",lower);
printf("num is %d\n",num);
printf("space is %d\n",space);
system("pause");
return 0;
}
统计字母,数字,空格的个数
最新推荐文章于 2023-03-02 14:08:09 发布