1. 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。(C语言)

如题所述

#include <stdio.h>

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]<='9' && s[i]>='0')

num++;

else if(s[i]<='z' && s[i]>='a' || s[i]<='Z' && s[i]>='A')

ch++;

else

n++;

i++;

}

printf("刚才输入的字符中英文字符个数为 %d\n", ch);

printf("刚才输入的字符中空格个数为 %d\n", space);

printf("刚才输入的字符中数字个数为 %d\n", num);

printf("刚才输入的字符中其他个数为 %d\n", n);

return 0;

}

扩展资料:    

while 循环的格式:while (表达式){语句;}

while 循环的执行顺序:当表达式为真,则执行下面的语句,语句执行完之后再判断表达式是否为真,如果为真,再次执行下面的语句,然后再判断表达式是否为真……就这样一直循环下去,直到表达式为假,跳出循环

例:

int a=NULL;

while(a<10){

a++;//自加

if(a>5)//不等while退出循环,直接判断循环

{break;//跳出循环}

}

结果: 结束后 a的值为6 。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2018-03-30

编程为:

#include <stdio.h>

int main(){     char c[50];     int i,el=0,sp=0,nu=0,other=0;      gets(c);//输入字符串            for(i=0; i<strlen(c); i++)//strlen返回字符串长度      {             if((c[i]>='A' && c[i]<='Z')||(c[i]>='a' && c[i]<='z'))                  el++;             else if(c[i]>='0'&&c[i]<='9')                 nu++;             else if(c[i]==' ')                 sp++;             else                   other++;     }     printf("英文字母个数=%d\n数 字 个 数 =%d\n空 格 字 数 =%d\n其他字符个数=%d\n",el,nu,sp,other);     return 0;}

已经测试过了,测试结果如下:

本回答被网友采纳
第2个回答  推荐于2017-11-17
#include <stdio.h>
void main()
{
 int letter, space, digit, other;
 char ch;
 letter = space = digit = other = 0;
 while ((ch = getchar ()) != '\n')
 {
  if (ch>='a' && ch <= 'z' || ch>='A'&&ch<='Z')
   letter++;
  else if (ch>='0' && ch <='9')
   digit++;
  else if (ch == ' ')
   space++;
  else
   other++;
 }
 printf ("字母:%d\n", letter);
 printf ("空格:%d\n", space);
 printf ("数字:%d\n", digit);
 printf ("其它字符:%d\n", other);
}

本回答被网友采纳
第3个回答  2018-10-17
#include<stdio.h>
int main()
{char c;
int letters=0,space=0,digit=0,other=0;
printf("请输入一行字符:I am a student.\n");
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
other++;}
printf("字母数:%d\n空格数:%d\n数字数:%d\n其他字符数:%d\n",letters,space,digit,other);
return 0;}
第4个回答  2020-02-04
#include <stdio.h>
int main(int argc, char *argv[])
{
int i[4]={0,0,0,0};
char a;
while((a=getchar())!='\n')
{
if(a>='0'&&a<='9') i[0]++;//数字
else if((a>='a'&&a<='z')||(a>='A'&&a<='Z')) i[1]++;//字母
else if(a==' ') i[2]++;//空格
else i[3]++;//其他字符
}
printf("您输入的数字有%d个\n"
"您输入的字母有%d个\n"
"您输入的空格有%d个\n"
"您输入的其它字符有%d个\n",i[0],i[1],i[2],i[3]);
return 0;
}