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

#include<stdio.h>intmain(){intletter=0,space=0,number=0,others=0;charnextchar;printf(... #include <stdio.h>
int main()
{
int letter=0,space=0,number=0,others=0;
char nextchar;
printf("Input your string\n");
for(;nextchar!='\n';)
{
scanf("%c",&nextchar);
if('a'<=nextchar<='z'||'A'<=nextchar<='Z')
letter++;
else if(nextchar==' ')
space++;
else if('0'<=nextchar<='9')
number++;
else
others++;
}
printf("letter=%d,space=%d,number=%d,others=%d\n",letter,space,number,others);
}

哪错啦
展开
 我来答
呆萌小咖77
2016-10-31 · TA获得超过797个赞
知道小有建树答主
回答量:813
采纳率:96%
帮助的人:139万
展开全部
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')//回车键结束输入,并且回车符不计入
{
if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
{
char_num++;
}
else if(ch==' ')
{
kongge_num++;
}
else if(ch>='0'&&ch<='9')
{
int_num++;
}
else
{
other_num++;
}
}
printf("字母= %d,空格= %d,数字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);
return 0;
}

2 ,do while语句:

#include<stdio.h>
int main(void)
{
//输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
char ch;
int char_num=0,kongge_num=0,int_num=0,other_num=0;
do
{
if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
{
char_num++;
}
else if(ch==' ')
{
kongge_num++;
}
else if(ch>='0'&&ch<='9')
{
int_num++;
}
else
{
other_num++;
}
} while((ch=getchar())!='\n')//回车键结束输入,并且回车符不计入
printf("字母= %d,空格= %d,数字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
心云细雨
推荐于2018-02-22 · TA获得超过687个赞
知道小有建树答主
回答量:316
采纳率:0%
帮助的人:330万
展开全部
#include <stdio.h>
int main()
{
int letter=0,space=0,number=0,others=0;
char nextchar;
printf("Input your string\n");
for(;nextchar!='\n';)
{
scanf("%c",&nextchar);
if(('a'<=nextchar&&nextchar<='z')||('A'<=nextchar&&nextchar<='Z'))
letter++;
else if(nextchar==' ')
space++;
else if('0'<=nextchar&&nextchar<='9')
number++;
else
others++;
}
printf("letter=%d,space=%d,number=%d,others=%d\n",letter,space,number,--others);
}
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
manchaozhao
2008-11-14 · TA获得超过204个赞
知道答主
回答量:134
采纳率:0%
帮助的人:0
展开全部
#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;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
FtoF
2010-07-26 · TA获得超过236个赞
知道答主
回答量:45
采纳率:0%
帮助的人:0
展开全部
public static void main(String[] args) {
// TODO Auto-generated method stub
int abcCount=0;//英文字母个数
int spaceCount=0;//空格键个数
int numCount=0;//数字个数
int otherCount=0;//其他字符个数
Scanner scan=new Scanner(System.in);
String str=scan.nextLine();
char[] ch = str.toCharArray();
for(int i=0;i<ch.length;i++){
if(Character.isLetter(ch[i])){
//判断是否字母
abcCount++;
}
else if(Character.isDigit(ch[i])){
//判断是否数字
numCount++;
}
else if(Character.isSpaceChar(ch[i])){
//判断是否空格键
spaceCount++;
}
else{
//以上都不是则认为是其他字符
otherCount++;
}
}
System.out.println("字母个数:"+abcCount);
System.out.println("数字个数:"+numCount);
System.out.println("空格个数:"+spaceCount);
System.out.println("其他字符个数:"+otherCount);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
非凡DZ
2007-06-13 · TA获得超过175个赞
知道答主
回答量:78
采纳率:0%
帮助的人:42.4万
展开全部
#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++;
}
else if(line[i]==' ')
{
count2++;
}
else if(line[i]>='0' && line[i]<='9')
{
count3++;
}
else
count4++;
i++;
}
printf("\n其中的英文字母个数为 %d\n",count1);
printf("\n其中的空格个数为 %d\n",count2);
printf("\n其中的数字个数为 %d\n",count3);
printf("\n其中的其他字符个数为 %d\n",count4);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式