您的位置:首页 > 编程语言 > C语言/C++

C语言算法小练习-9

2015-09-17 10:43 239 查看

题目及程序:

[code]/**
 10.    题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。(程序分析:利用while语句,条件为输入的字符不为'\n'.)
**/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
    int letter=0,number=0,space=0,other=0;
    char c;
//    char *string=malloc(100*sizeof(char));//假设字符串长度为100
    printf("请输入一行字符:\n");

    while ((c=getchar())!='\n') {
        if ((c>='A'&&c<='Z')||(c>='a'&&c<='z')) {
            letter++;
        }else if(c>='0'&&c<='9'){
            number++;
        }else if(c==' '){
            space++;
        }else{
            other++;
        }
    }
    printf("letter=%d,number=%d,space=%d,other=%d\n",letter,number,space,other);
    return 0;
}


运行:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: