当前位置: 代码迷 >> C语言 >> [求助]请教:关于统计问题
  详细解决方案

[求助]请教:关于统计问题

热度:235   发布时间:2006-08-18 19:46:58.0
[求助]请教:关于统计问题
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
我什么想都不明,麻烦大哥大姐教一下啦!
搜索更多相关的解决方案: 统计  数字  字符  

----------------解决方案--------------------------------------------------------
每个字符都有Ascal码,你可以根据它来判断字符是属于哪一类的

比如数字0-9的Ascal码是48-57
----------------解决方案--------------------------------------------------------
空格的AS 码值是 32
自己想想吧

----------------解决方案--------------------------------------------------------
以下是引用世外水源在2006-8-18 19:46:58的发言:
输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
我什么想都不明,麻烦大哥大姐教一下啦!

我用C++帮你做了出来了,留了空间让你自己想一下怎样把它改成C语言的版本!努力哦!~
#include <iostream>
using namespace std;
int main()
{int letter=0,space=0,number=0,other=0;
char c;
while((c=getchar())!='\n')
{if(c>='a'&&c<='z'||c>='A'&&c<='Z') letter++;
else if(c==' ') space++;
else if(c>='0'&&c<='9') number++;
else other++;
}
cout<<"letter="<<letter<<endl;
cout<<"space="<<space<<endl;
cout<<"number="<<number<<endl;
cout<<"other="<<other<<endl;
system("pause");
}
以上程序在DEV C++4.9版本里编译通过!


----------------解决方案--------------------------------------------------------
#include<stdio.h>
#include<string.h>
int main(void)
{
char array[100];
int i=0,len=0,count=0,count1=0,count2=0,count3=0;
gets(array);
len=strlen(array);
for(i=0;i<len;i++){
if(array[i] >='a' && array[i] <= 'z' || array[i] >= 'A' && array[i] <= 'Z')
count++;
else if(array[i]==' ')
count1++;
else if(array[i]>= '0' && array[i]<= '9')
count2++;
else
count3++;
}
printf("the number of letter is%i\n the number of blank is%i\n the number of number is%i\n the number of others is%i\n",count,count1,count2,count3);
getch();
return 0;
}

呵呵 小试以下
----------------解决方案--------------------------------------------------------
  相关解决方案