C语言编程:输入一行字符,统计其中英文字母的个数?

如题所述

#include<stdio.h>

int main()

{char s[200];

int i,n=0;

gets(s);

for(i=0;s[i];i++)

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

printf("%d\n",n);

getch();

return 0;

}

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2016-08-18
题目:输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
1.程序分析:利用while语句,条件为输入的字符不为'\n'.
      
2.程序源代码:
#include "stdio.h"
#include "conio.h"
main()
{
char c;
int letters=0,space=0,digit=0,others=0;
printf("please input some characters\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
others++;
}
printf("all in all:char=%d space=%d digit=%d others=%d\n",letters,
space,digit,others);
getch();
}本回答被提问者采纳
第2个回答  2011-09-22
#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;
}

第3个回答  2011-10-05
#include <stdio.h>
void main()
{
int letter, space, number, other;
char ch;
letter = space = number = other = 0;
while((ch = getchar()) != '\n')
if('A' <= ch && ch <= 'Z' || 'a' <= ch && ch <= 'z') letter++;
else
if(ch == ' ') space++;
else
if('0' <= ch && ch <= '9') number++;
else
other++;
printf("letter:%d\n", letter);
printf("space:%d\n", space);
printf("number:%d\n", number);
printf("other:%d\n", other);
}
第4个回答  2011-09-22
112aaBB..
有英文字母4个。
Press any key to continue

#include<stdio.h>
#include "string.h"
main()
{
int i,nLen,COUNT=0;
char aa[100];
gets(aa);
nLen = strlen(aa);
for (i=0;i<nLen;i++)
{
if ((aa[i]>='a' && aa[i]<='z') || (aa[i]>='A' && aa[i]<='Z'))
{
COUNT++;
}
}
printf("有英文字母%d个。\n",COUNT);
}

有疑问请追问 满意记得采纳

相关了解……

你可能感兴趣的内容

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