分别统计出在输入的字符串中字母,数字和其他字符的个数?

2024-12-31 17:57:44
推荐回答(2个)
回答1:

#include

/* count digits, char, others */
main()
{
int c, i, nchar, nother;
int ndigit[10];

nchar = nother = 0;
for (i = 0; i < 10; ++i)
ndigit[i] = 0;

while ((c = getchar()) != EOF)
if (c >= '0' && c <= '9')
++ndigit[c-'0'];
else if (c >= 'a' && c <= 'z' || c >='A' && c<='Z')
++nchar;
else
++nother;
printf("digits =");
for (i = 0; i < 10; ++i)
printf(" %d", ndigit[i]);
printf(", white space = %d, other = %d\n", nchar, nother);
}

回答2:

#include "stdio.h"

void main( void )
{
char string[50];
int length,i,num;

printf("Please input the string");
gets(string);

length=strlen(string);
num=0;
for(i=0;i{
if(string[i]<='9'&&string[i]>='0')
num++;
}
printf("\nThere are %d num",num);
printf("\n%d other charactors",strlen-num);
}