输入一行字符,分别统计出其中英文字母,空格,数字字符,其它字符及单词的个数.

如题所述

#include<stdio.h>

int main()

{

char c;

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

printf("请输入一串任意的字符:\n");

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

{

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

letters++;

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

digits++;

else if(c==' ')

spaces++;

else

others++;

}

printf("字母有%d个,数字有%d个,空格有%d个,其他有%d个",letters,digits,spaces,others);

return 0;

}

扩展资料:

while语句若一直满足条件,则会不断的重复下去。但有时,需要停止循环,则可以用下面的三种方式:

一、在while语句中设定条件语句,条件不满足,则循环自动停止。

如:只输出3的倍数的循环;可以设置范围为:0到20。

二、在循环结构中加入流程控制语句,可以使用户退出循环。

1、break流程控制:强制中断该运行区内的语句,跳出该运行区,继续运行区域外的语句。

2、continue流程控制:也是中断循环内的运行操作,并且从头开始运行。

三、利用标识来控制while语句的结束时间。

参考资料来源:

百度百科——while

温馨提示:答案为网友推荐,仅供参考
第1个回答  2020-03-24
第2个回答  2020-03-09

C语言经典例子之统计英文、字母、空格及数字个数

第3个回答  2014-04-10
 #include <stdio.h>
 int main()
 {
     char s[100], *p;
     int chars = 0, nums = 0, bs = 0, others = 0;
         printf("请输入一个字符串:");
     scanf("%s",s);
     p =s;
     while (*p) {
         if ('a' <= *p && *p <= "z') || ('A' <= *p && *p <= "Z') {
             chars++;
           } 

 

追问

就这么多么?

第4个回答  推荐于2017-12-16
//C语言代码:
#include <stdio.h>
int main()
{
    char c;
    int letters = 0, space = 0, digit = 0,others = 0,words = 0;
    bool flag;
    printf("请输入一行字符:");
    while((c = getchar()) != '\n')
    {
        if(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z')
            letters++;
        else if(c == ' ')  
            space++;
        else if(c >= '0' && c <= '9')
            digit++;
        else
            others++;
        if( c == ' ')
            flag = false;
        else if(flag == false)
        {
            flag = true;
            words++;
        }
    }
    printf("字母数:%d\n空格数:%d\n数字数:%d\n其它字符:%d\n单词数:%d\n",letters,space,digit,others,words);
}

本回答被提问者采纳