C语言实现
#include<stdio.h>
void main()
{
int abc = 0;
int num = 0;
int space = 0;
int other = 0;
char str;
printf("请输入要统计的字符串");
while ((str=getchar())!='\n')
{
if ((str >= 'a'&&str <= 'z') || str >= 'A'&&str <= 'Z')
abc++;
else if (str == ' ')
space++;
else if (str >= '0'&&str <= '9')
num++;
else
other++;
}
printf("字母的数量:%d\n",abc);
printf("数字的数量:%d\n", num);
printf("空格的数量:%d\n", space);
printf("其他:%d\n", other);
}
运行结果:
请输入要统计的字符串the num 520 means I Love You @young people
字母的数量:30
数字的数量:3
空格的数量:8
其他:1
请按任意键继续. . .
python语言实现
str1 = str(input("请输入要统计的字符串:"))
abc = 0
num = 0
space = 0
other = 0
for i in range(0,len(str1)):
if str1[i] >="a" and str1[i] <= "z":
abc+=1
elif str1[i] >="A" and str1[i] <= "Z":
abc+=1
elif str1[i] >="0" and str1[i] <= "9":
num+=1
elif str1[i] == ' ':
space+=1
else:
other+=1
print("英文字母:%d"% abc)
print("数字:%d"% num)
print("空格:%d"% space)
print("其他:%d"% other)
'''
将ASCII字符转换为对应的数值即‘a’-->65,使用ord函数,ord('a')
反正,使用chr函数,将数值转换为对应的ASCII字符,chr(65)
'''
运行结果:
请输入要统计的字符串:the num 520 means I Love You @young people
英文字母:30
数字:3
空格:8
其他:1
★finished by songpl,2018.12.19