求一个c语言程序,输入一串字符,以'#'结尾,并统计其中数字,字母,空格和其他字符的个数

如题所述

下面是完整程序。输入一串 字符,可以是 大小写字母,数字,符号(含任意个数 回车,换行,空白 等等) ,一旦拍入 #号,就输出 数字个数,字母个数,空格 和其它
#include <stdio.h>
int main(){
char s[1000];
int c;
int i,n=0;
int n_num=0, n_letter=0, n_sp=0, n_other;
printf("input your strings and press # as the end:\n");
while(1){
c = _getch();
if (c == '#') {s[n]='\0'; break;};
if (c ==' ') n_sp++;
else if (c>='0' && c <='9') n_num++;
else if ( (c>='a' && c<='z') || (c>='A' && c<='Z')) n_letter++;
else n_other++;
s[n]=c;n++;
}
printf("n_num=%d n_letter=%d n_sp=%d n_other=%d\n",n_num, n_letter,n_sp,n_other);
printf("you input was:\n%s\n",s);
//for (i=0;i<n;i++) printf("%c",s[i]);
return 0;
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2016-02-09
#include<stdio.h>
int main()
{
    char c;
    int num=0,lett=0,bar=0,others=0;
    scanf("%c",&c);
    while(c!='#')
    {
        if(c==' ') bar++;
        else if(c>='0'&&c<='9') num++;
        else if(c>='a'&&c<='z' || c>='A'&&c<='Z') lett++;
        else others++;
        scanf("%c",&c);
    }
    return 0;
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网