统计字符个数
能帮忙看看这个吗
有点小困难
根本不知道怎么写
帮帮我
用gets函数接收一行字符。或者用getchar()逐个字符接收,然后判断字符范围进行统计
#include <stdio.h>
int main()
{
int letter = 0,black = 0,digit = 0,other = 0;
char c;
printf("Input 10 characters: ");
while((ch=getchar()) != '\n')
{
if((ch >='a' && ch <='z') || (ch>='A' && ch<='Z'))
letter++;
else if(ch == ' ')
black++;
else if(ch>='0' && ch<='9')
digits++;
else
others++;
}
printf("letter=%d,black=%d,digit=%d,other=%d",letter,black,digit,other);
}
供参考:
#include<stdio.h>
int main()
{
char ch;
int i, letter = 0, digit = 0, blank = 0, other = 0;
printf("Input 10 characters: ");
for (i = 0; i < 10; i++)
{
ch = getchar();
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
letter++;
else if (ch >= '0' && ch <= '9')
digit++;
else if (ch == ' ' || ch == '\n')
blank++;
else
other++;
}
printf("letter=%d,blank=%d,digit=%d,other=%d", letter, blank, digit, other);
return 0;
}