Add 字符分类统计:从键盘输入一个长度为10的字符串,分别统计出其中大写字母、小写字母、数字、空格和其他字符的个数并分别输出

master
pec9ut8xf 4 years ago
parent cbede7d94f
commit e87afc7dcf

@ -0,0 +1,40 @@
#include<stdio.h>
#include<string.h>
int main()
{
char str[10];
gets(str);
int cap = 0,low = 0,num = 0,space = 0,other = 0;
int i;
int len = strlen(str);
for(i = 0;i < len;i++)
{
if(str[i] >= 'A'&&str[i] <= 'Z')
{
cap++;
}
else if(str[i] >= 'a'&&str[i] <= 'z')
{
low++;
}
else if(str[i] >= '0'&&str[i] <= '9')
{
num++;
}
else if(str[i] == ' ')
{
space++;
}
else
{
other++;
}
}
printf("大写字母:%d\n小写字母%d\n数字%d\n空格%d\n其他%d\n",cap,low,num,space,other);
/**
时间复杂度: 字符分类统计若从键盘输入一个长度为n的字符串分别统计出其中大写字母、小写字母、数字、空格和其他字符的个数并分别输出
当n在某个特定的范围内在最优的情况下其时间复杂度为O(n);
空间复杂度字符分类统计的空间复杂度为S(n);
**/
}
}
Loading…
Cancel
Save