输入一行字符,分别统计出其中的英文字母、空格、数字、和其他字符的个数,用C语言编写程序

作者&投稿:尹慧 (若有异议请与网页底部的电邮联系)
1.\t输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。(C语言)~

#include
int main()
{
int i=0, space=0, num=0, n=0, ch=0;
char s[20];
printf("请输入一串字符 ");
gets(s);
while(s[i] != '\0')
{
if(s[i]==' ')
space++;
else if(s[i]='0')
num++;
else if(s[i]='a' || s[i]='A')
ch++;
else
n++;
i++;
}
printf("刚才输入的字符中英文字符个数为 %d
", ch);
printf("刚才输入的字符中空格个数为 %d
", space);
printf("刚才输入的字符中数字个数为 %d
", num);
printf("刚才输入的字符中其他个数为 %d
", n);
return 0;
}

扩展资料:
while 循环的格式:while (表达式){语句;}
while 循环的执行顺序:当表达式为真,则执行下面的语句,语句执行完之后再判断表达式是否为真,如果为真,再次执行下面的语句,然后再判断表达式是否为真……就这样一直循环下去,直到表达式为假,跳出循环。
例:
int a=NULL;
while(a<10){
a++;//自加
if(a>5)//不等while退出循环,直接判断循环
{break;//跳出循环}
}
结果: 结束后 a的值为6 。

非要限制输入的大小么?过会儿给你发个
#include
#include
void main(){
char m_input;
int digit=0,space=0,others=0,uppercase=0,lowercase=0;
printf("Please input string:");
while ((m_input=getchar())!='
')
{
if (m_input>='a'&&m_input<='z')
lowercase++;
else if (m_input>='A'&&m_input<='Z')
uppercase++;
else if (m_input>'0'&&m_input<'9')
digit++;
else if (m_input==' ')
space++;
else
others++;
}
printf("lowercase:%d uppercase:%d digit:%d space:%d others:%d
",lowercase,uppercase,digit,space,others);
}

一、问题分析:

输入一行字母,那么会以换行结束。所以可以存入数组,也可以逐个输入,遇到换行结束。

要统计各个类的个数,就要逐个判断是哪个分类的。

由于在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())!='
')//循环读取字符,到换行结束。
    {
        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
", a,b,c,d);//输出结果。
    return 0;
}


#include <stdio.h>
#include <string.h>
#define A 80
main()
{
char str[A];
int len,i,letter=0,digit=0,space=0,others=0;
printf("请输入一行字符:");
gets(str);
len=strlen(str);
for(i=0;i<len;i++)
{
if(str[i]>='a'&&str[i]<='z'||str[i]>='A'&&str[i]<='Z')
letter++;
else if(str[i]>='0'&&str[i]<='9')
digit++;
else if(str[i]==' ')
space++;
else
others++;
}
printf("英文字符有:%d\n",letter);
printf("数字字符有:%d\n",digit);
printf("空格有:%d\n",space);
printf("其他字符有:%d\n",others);
}

用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);
}

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



#include<stdio.h>
int main()
{
int letter=0,blank=0,digit=0,other=0;
char c;
printf("Input 10 characters: ");
while((c=getchar())!='\n')
{
if(c>='a'&&c<='z'||c>='A'&&c<='Z')
{letter++;}
else if(c==' ')
{blank++;}
else if(c>='0'&&c<='9')
{digit++;}
else
{other++;}
}
printf("letter=%d,blank=%d,digit=%d,other=%d\n",letter,blank,digit,other);
return 0;
}


山海关区17617071434: 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数 -
调店敬宇: #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...

山海关区17617071434: 用C语言编程:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数. -
调店敬宇: #include <stdio.h> 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]<=122))||((line[i]>=65) && (line[i]<=90))){count1++;}...

山海关区17617071434: 编程题: 输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数.(分别使用wh -
调店敬宇: 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')//回车键结束输入,并且...

山海关区17617071434: 输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数. -
调店敬宇: #include<stdio.h>#include<stdlib.h>#include<string.h> int i,a[4]; char t; int main() {for(i=0;;i++){ scanf("%c",&t); if(t=='\n')break; else if(t>='A'&&t<='Z') a[0]++; else if(t>='a'&&t<='z')a[0]++; else if(t==' ')a[1]++;else if(t>='0'&&t<='9')a[2]++; else a[3]+...

山海关区17617071434: 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符 -
调店敬宇: 程序如下所示,仅供参考: #includevoid hlw(char *s){ int zimu=0,shuzi=0,kongge=0,qita=0; while(*s){ if(*s>='a'&&*s<='z'||*s>='A'&&*s<='Z') zimu++; else if(*s>='0'&&*s<='9') shuzi++; else if(*s==' ') kongge++; else qita++; s++; } printf("\n\n输入的字...

山海关区17617071434: 输入一行字符,分别统计出其中的英文字母.空格.数字.和其他字符的个数! -
调店敬宇: #include <stdio.h>#include <ctype.h> /*for type hceck*/#include <string.h>/*for strlen()*/ int main() { int i; unsigned letters = 0; /*字母*/ unsigned spaces = 0; /*空格*/ unsigned digits = 0; /*数字*/ unsigned others = 0; /*其他*/ char line[256]; printf...

山海关区17617071434: c语言函数解 答输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数 求下面解答!!1 -
调店敬宇: //楼上太复杂了吧 void char_stat(char *str,int *counter ) {char ch,i;for(i=0;str[i]!='\0';i++){if(str[i]>=65&&str[i]<=90)counter[0]++;else if(str[i]>=97&&str[i]<=122)counter[0]++;else if(str[i]>=48&&str[i]<=57)counter[2]++;else if(str[i]==32)counter[...

山海关区17617071434: 输入一行字符,分别统计其中英文字母、空格、数字、和其他字符的个数?
调店敬宇: #include 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='a') { char_num++; } else if(ch==' ') { kongge_num++; } else if(ch>='0'&&ch 全部

山海关区17617071434: C语言题目输入一行字符,分别统计出其中英文字母,空格,数字和其他字符的个数. -
调店敬宇: 错误代码: 1.'a'<=nextchar<='z'||'A'<=nextchar<='Z'; 2.'0'<=nextchar<='9'. 错误原因:当多个条件时,需要使用逻辑运算符. 修改后代码为: int main(void){ int letters = 0, spaces = 0, digits = 0, others = 0; char c; printf("输入一行字符串:\n"...

山海关区17617071434: 5.1输入一行字符,分别统计出其中的英文字母、数字、空格和其它字符的个数. -
调店敬宇: 一、问题分析:输入一行字母,那么会以换行结束.所以可以存入数组,也可以逐个输入,遇到换行结束.要统计各个类的个数,就要逐个判断是哪个分类的.由于在ASCII码中,数字,大写字母,小写字母分别连续,所以可以根据边界值判断...

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