编程题: 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。(分别使用wh

编程题:
输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数。(分别使用while语句和do while语句实现)

1 while语句:

#include<stdio.h>
int main(void)
{
//输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
char ch;
int char_num=0,kongge_num=0,int_num=0,other_num=0;
while((ch=getchar())!='\n')//回车键结束输入,并且回车符不计入
{
if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
{
char_num++;
}
else if(ch==' ')
{
kongge_num++;
}
else if(ch>='0'&&ch<='9')
{
int_num++;
}
else
{
other_num++;
}
}
printf("字母= %d,空格= %d,数字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);
return 0;
}

2 ,do while语句:

#include<stdio.h>
int main(void)
{
//输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
char ch;
int char_num=0,kongge_num=0,int_num=0,other_num=0;
do
{
if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
{
char_num++;
}
else if(ch==' ')
{
kongge_num++;
}
else if(ch>='0'&&ch<='9')
{
int_num++;
}
else
{
other_num++;
}
} while((ch=getchar())!='\n')//回车键结束输入,并且回车符不计入
printf("字母= %d,空格= %d,数字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-04-17

代码:

    #include <stdio.h>

    void main()

    {

    int letter=0,space=0,digit=0,other=0;

    char c;

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

    {

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

    letter++;

    else if(c==' ')

    space++;

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

    digit++;

    else

    other++;

    }

    printf("英文字母:%d\n",letter);

    printf("空格:%d\n",space);

    printf("数字:%d\n",digit);

    printf("其它字符:%d\n",other);

    }

简介:

编程,是让计算机为解决某个问题,而使用某种程序设计语言编写程序代码,并最终得到结果的过程。为了使计算机能够理解人的意图,人类就必须要将需解决的问题的思路、方法、和手段通过计算机能够理解的形式告诉计算机,使得计算机能够根据人的指令一步一步去工作,完成某种特定的任务。这种人和计算机之间交流的过程就是编程。

计算机语言的种类非常多,可分成机器语言,汇编语言,高级语言三大类。计算机对除机器语言以外的源程序不能直接识别、理解和执行,都必须通过某种方式转换为计算机能够直接执行的。程序设计语言编写的源程序转换到机器目标程序有:解释方式和编译方式两种。

本回答被网友采纳
第2个回答  2013-11-14
#include
void main()
{
char line[30];
int i,count1=0,count2=0,count3=0,count4=0;
printf("\n请输入一行字符: ");
gets(line);
i=0;
while(line[i]!='\0')
{
if(((line[i]>=97) && (line[i]=65) && (line[i]<=90)))
{
count1++;
}
else if(line[i]==' ')
{
count2++;
}
else if(line[i]>='0' && line[i]<='9')
{
count3++;
}
else
count4++;
i++;
}
printf("\n其中的英文字母个数为 %d\n",count1);
printf("\n其中的空格个数为 %d\n",count2);
printf("\n其中的数字个数为 %d\n",count3);
printf("\n其中的其他字符个数为 %d\n",count4);
}
第3个回答  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;
}

第4个回答  2010-04-01
用for语句编的.....

#include<stdio.h>
void main()
{
int z,k,s,q;
char ch;
z=k=s=q=0;
for(ch=getchar();ch!='\n';;)
{
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
z++;
else if(ch==' ')
k++;
else if(ch>='0'&&ch<='9')
s++;
else q++;
ch=getchar();
}
printf("zimu:%d\nspace:%d\nshuzi:%d\nqita:%d\n"z,k,s,q);
}
相似回答