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

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

哪错啦

第1个回答  2019-03-11
输入一行字符=input("请输入任意数据:")


数字个数=len(list(i for i in 输入一行字符 if i.isdigit()==1))
中英文字母个数=len(list((i for i in 输入一行字符 if i.isalpha()==1)))
空格个数=len(list(i for i in 输入一行字符 if i==" "))
其他个数=len(输入一行字符)-数字个数-中英文字母个数-空格个数
print("{0}中有{1}个数字,{2}个中英文字母,{3}个空格个数,{4}个其他".format(输入一行字符,数字个数,中英文字母个数,空格个数,其他个数))

第2个回答  2012-11-28
#include<stdio.h>
#define N 100
int main()
{
char a[N];
int i,m=0,n=0,b=0,c=0;
printf("Input a string:");
gets(a);
for(i=0;a[i]!='\0';i++)
{
if(a[i]>='a'&&a[i]<='z'||a[i]>='A'&&a[i]<='Z')
m++;
else if(a[i]>='0'&&a[i]<='9')
n++;
else if(a[i]==' ')
b++;
else
c++;
}
printf("英文字母:%d\n",m);
printf("数字字符:%d\n",n);
printf("空格:%d\n",b);
printf("其他字符:%d\n",c);
return 0;
}
第3个回答  2013-01-03
你好:

你定义字符数组 char a[50] 时,意味着这个字符串最多能容纳 50 个字符,并不是说一定是 50 个字符。(实际上是 49 个字符,因为作为字符串要有一个结束标识 '\0' 的)

你可以借助 string.h 头文件里的 strlen 函数来测量字符串的长度。

程序修改如下:

#include <stdio.h>
#include <string.h>

void main(){
int i, l;
char a[50];
char c;
int letter=0, blank=0, number=0, other=0;
gets(a);
l=strlen(a); //测量字符串 a 的实际长度
for(i=0;i<l;i++){ //遍历字符串中的每一个字符
c=a[i]; //取出下标为 i 处的字符
if((c>='A' && c<='Z') || (c>='a' && c<='z')){
letter++;
}
else if(c>='0' && c<='9'){
number++;
}
else if(c==' '){
blank++;
}
else{
other++;
}
}
printf("Letter: %d, Number: %d, Blank: %d, Other: %d\n",letter,number,blank,other);
}
第4个回答  2014-02-06

其中一种方法:

            Console
.Write(
"请输入字符:"
);
            
string
 s = 
Console
.ReadLine();
            
while
 (!
string
.IsNullOrEmpty(s))
            {
                
int
 i = 0;
                
int
 j = 0;
                
int
 m = 0;
 
                
foreach
 (
char
 ch 
in
 s)
                {
                    
short
 temp = 
Convert
.ToInt16(ch);
                    
if
 (temp >= 48 && temp <= 57)
                    {
                        i++;
                    }
                    
else
 
if
 ((temp >= 65 && temp <= 90) || (temp >= 97 && temp <= 122))
                    {
                        j++;
                    }
                    
else
                    {
                        m++;
                    }
                }
                
Console
.WriteLine(
"您输入的字符:{0}中,数字个数:{1},字母个数:{2},其他字符个数:{3}"
, s, i, j, m);
                
Console
.Write(
"请输入字符:"
);
                s = 
Console
.ReadLine();
            }

第5个回答  2012-11-28
这个问题,在《C语言程序设计》清华大学出版社中有提到,在P78,下面附上代码,具体情况你自己看吧:
//输入一行字符,统计并输出其中的英文字符,数字字符,空格和其他字符的个数
#include"stdio.h"
void main()
{
char ch;
//letter英文字符,space数字字符,digit空格,other其他字符
int letter=0,space=0,digit=0.other=0;
printf("请输入一行字符:\n");
while((ch=getchar())!='\n') //设置循环条件为输入的字符不是回车符
if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
letter++;
else if(ch==' ')
space++;
else if(ch>='0'&&ch<='9')
digit++;
else
other++;
printf("字符%d个,空格%d个,数字%d个,其它字符%d个",letter,space,digit,other);
}