如何输入一行字符,分别统计出其中英文字母,空格?

是这样的,想请教下,如何输入一行字符,分别统计出其中英文字母,空格?
最新回答
和旋帅哥

2024-06-22 03:30:25

错误代码:

if('a'<=nextchar<='z'||'A'<=nextchar<='Z')

else if('0'<=nextchar<='9')

修改后:

#include <stdio.h>

int main()

{

int letter=0,space=0,number=0,others=0;

char nextchar;

printf("Input your string\n");

for(;nextchar!='\n';)

{

scanf("%c",&nextchar);

if('a'<=nextchar&&nextchar<='z'||'A'<=nextchar&&nextchar<='Z')

letter++;

else if(nextchar==' ')

space++;

else if('0'<=nextchar&&nextchar<='9')

number++;

else

others++;

}

printf("letter=%d,space=%d,number=%d,others=%d\n",letter,space,number,others);

}

c++输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。

#include<cstdio>

int main()

{

char x[999];

int i,a=0,b=0,c=0,d=0;

gets(x);

for(i=0;i<=x[i];i++)

{

if('A'<=x[i]&&x[i]<='z')

a++;

else if('0'<=x[i]&&x[i]<='9')

b++;

else if(x[i]==' ')

c++;

else

d++;

}

printf("%d %d %d %d\n",a,b,c,d);

return 0;

}