任意输入一行字符,分别统计字母,数字,空格和其他字符的个数(c语言),带有结果的

谢谢

#include<stdio.h>
int main()
{
    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);
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-01-09
//以前写的code,测试通过,如果有疑问,欢迎交流
#include<stdio.h>

void tar_func(char * tar){
int eng_c = 0, dig_c = 0, spc_c = 0, oth_c = 0;
int count = 0;
puts("输入的字符串:");
puts(tar);
while(tar[count] != '\0'){
if(tar[count] >= 'a' && tar[count] <= 'z' || tar[count] >= 'A' && tar[count] <='Z')
eng_c++;
else if(tar[count] >='0' && tar[count] <= '9')
dig_c++;
else if(tar[count] == ' ')
spc_c++;
else
oth_c++;
count ++;
}
printf("字符串中的英文字母有%d个\n", eng_c);
printf("数字有%d个\n", dig_c);
printf("空格有%d个\n", spc_c);
printf("其它字符有%d个\n", oth_c);

}

int main(){
char tar[1000]; 
gets(tar);
tar_func(tar);
return 0;
}

第2个回答  2015-01-10
#include "stdio.h"
void main()
{
char s;
int i=0,j=0,k=0,m=0,da=0,xiao=0;
printf("please input the string\n");
while((s=getchar())!='\n') /*循环从键盘读入字符直到一行结束(输入回车)*/
{

if((s='a')||(s'A'))
{
if(s='A')da++;
if(s='a')xiao++;
i++; /*i存入字母数*/
}
else if(s==' ') j++; /*j存入空格数,注意s==' '里面是有一个空格的*/
else if(s47)k++; /*k存入数字数*/
else m++; /*m存入其它符号数*/
}
printf("char:%d Capital letters:%d Lowercase%d\nspec:%d\nnumber:%d\nOther:%d\n",i,da,xiao,j,k,m); /*打印行中的字母,空格,数字,其它字符数*/
}
第3个回答  2018-07-03

开头加#include <conio.h>

然后,完善了下(不要喷,谢谢)

相似回答