中易网

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

答案:2  悬赏:70  
解决时间 2021-01-18 18:35
  • 提问者网友:浩歌待明月
  • 2021-01-17 17:37
求一个c语言程序,输入一串字符,以'#'结尾,并统计其中数字,字母,空格和其他字符的个数
最佳答案
  • 二级知识专家网友:独行浪子会拥风
  • 2021-01-17 18:34
下面是完整程序。输入一串 字符,可以是 大小写字母,数字,符号(含任意个数 回车,换行,空白 等等) ,一旦拍入 #号,就输出 数字个数,字母个数,空格 和其它
#include
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;ireturn 0;
}
全部回答
  • 1楼网友:duile
  • 2021-01-17 19:01
#include
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;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息!
大家都在看
推荐信息