C语言题目输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。

#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<='z'||'A'<=nextchar<='Z')
letter++;
else if(nextchar==' ')
space++;
else if('0'<=nextchar<='9')
number++;
else
others++;
}
printf("letter=%d,space=%d,number=%d,others=%d\n",letter,space,number,others);
}

哪错啦

错误代码:

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;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2019-05-17

错误代码:

1.'a'<=nextchar<='z'||'A'<=nextchar<='Z';

2.'0'<=nextchar<='9'。

错误原因:当多个条件时,需要使用逻辑运算符。

修改后代码为:

int main(void){

int letters = 0, spaces = 0, digits = 0, others = 0;

char c;

printf("输入一行字符串:\n");

while ((c = getchar()) != '\n')

{

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

{

letters++;

else if (c == ' ')

{

spaces++;

}

else if (c >= '0'&&c <= '9')

{

digits++;

}

else

{

others++;

}

}

printf("字母=%d,数字=%d,空格=%d,其他=%d\n", letters, digits, spaces, others);

return 0;

}

扩展资料:

逻辑运算符可以将两个或多个关系表达式连接成一个或使表达式的逻辑反转。本节将介绍如何使用逻辑运算符将两个或多个关系表达式组合成一个。

&& 运算符被称为逻辑与运算符。它需要两个表达式作为操作数,并创建一个表达式,只有当两个子表达式都为 true 时,该表达式才为 true。

|| 运算符被称为逻辑或运算符。它需要两个表达式作为操作数,并创建一个表达式,当任何一个子表达式为 true 时,该表达式为 true。

! 运算符被称为逻辑非运算符,执行逻辑 NOT 操作。它可以反转一个操作数的真值或假值。换句话说,如果表达式为 true,那么 ! 运算符将返回 false,如果表达式为 false,则返回 true。

参考资料:

百度百科-逻辑运算符

本回答被网友采纳
第2个回答  2017-06-21

一、问题分析:

输入一行字母,那么会以换行结束。所以可以存入数组,也可以逐个输入,遇到换行结束。

要统计各个类的个数,就要逐个判断是哪个分类的。

由于在ASCII码中,数字,大写字母,小写字母分别连续,所以可以根据边界值判断类型。

二、算法设计:

1、读入字符,直到遇到换行结束。

2、对于每个字符,判断是字母还是数字,或者空格,或者是其它字符。

3、对于每个字符判断后,对应类别计数器自加。

4、最终输出结果。

三、参考代码:

#include <stdio.h>
int main()
{
    int a,b,c,d,ch;
    a=b=c=d=0;//计数器初始化为0.
    while((ch=getchar())!='\n')//循环读取字符,到换行结束。
    {
        if(ch>='0' && ch<='9')//数字
            a++;
        else if((ch>='a' && ch<='z')||(ch>='A' && ch<='Z'))//字母
            b++;
        else if(ch==' ')//空格
            c++;
        else //其它
            d++;
    }
    printf("%d %d %d %d\n", a,b,c,d);//输出结果。
    return 0;
}

第3个回答  2010-05-18
#include<stdio.h>
void main()
{
//char a[50];
int letter=0,number=0,blank=0,other=0;
//int i;
//gets(a);
char c; 用来读取每个字符
while ((c=getchar())!='\n') //基本就是修改的这句,当读入的是回车即为结束运算
//for(i=0;i<50,a[i]='\n';i++)
{
if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))
letter++;
else if(c>='0'&&c<='9')
number++;
else if(c==' ')
blank++;
else other++;
}
printf("letter=%d,number=%d,blank=%d,other=%d",letter,number,blank,other);
}
程序已经验证过,可以执行哦~~~
第4个回答  2007-04-17
#include "stdio.h"
void main()
{
char s;
int i=0,j=0,k=0,m=0,da=0,xiao=0;
printf("please input the string\n");
while((s=getchar())!='\n') /*循环从键盘读入字符直到一行结束(输入回车)*/
{

if((s<='z'&&s>='a')||(s<'Z'&&s>'A'))
{
if(s<='Z'&&s>='A')da++;
if(s<='z'&&s>='a')xiao++;
i++; /*i存入字母数*/
}
else if(s==' ') j++; /*j存入空格数,注意s==' '里面是有一个空格的*/
else if(s<58&&s>47)k++; /*k存入数字数*/
else m++; /*m存入其它符号数*/
}
printf("char:%d Capital letters:%d Lowercase%d\nspec:%d\nnumber:%d\nOther:%d\n",i,da,xiao,j,k,m); /*打印行中的字母,空格,数字,其它字符数*/
}