帮忙做一道C语言的题目,在线等!急!

写一个函数,使其能统计主调函数通过实参传递来的字符串,对其中的字母,数字,空格分别计数(注意:要求在主函数中输入字符串及输出统计结果)。[参考函数原型:void count(char*str)]。
这是原题,帮我做一下,我知道这应该不会很难,因为我是C语言的新手,请大家帮帮忙,我也是百度知道的新手,所以分数很少,只有十五分,拜托。

)#include <stdio.h>
int letter; //字母个数
int digit; //数字个数
int space; //空格个数
int others; //其它字母个数
void main()
{
void count(char str[]); //统计个数的函数的声明
char s[81];
printf("请输入一个字符串:");
gets(s);
letter=0; digit=0;
space=0; others=0;
count(s);
printf("字符串中共有 %d 个字母,%d 个数字,%d 个空格,%d个其它字母。\n",letter,digit,space,others);
}
void count(char str[]) //统计个数的函数的定义
{
int i;
char c;
for(i=0; str[i]!='\0';i++)
{
c=str[i];
if(c>='a' && c<='z' || c>='A' && c<='Z')
{ letter++; }
else if(c>='0' && c<='9')
{ digit++; }
else if(c==' ')
{ space++; }
else
{ others++; }
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2009-05-03
#include <stdio.h>
int a=0,b=0,c=0;
void fun(char *s);
int main(void)
{ char s[100];
printf("Please input\n");
gets(s);
fun(s);
printf("字母:%d 数字:%d 空格:%d\n",a,b,c);
}
void fun(char *s)
{ int i;
for(i=0;s[i]!='\0';i++)
{ if((s[i]<='Z'&&s[i]>='A')||(s[i]<='z'&&s[i]>='a')) a++;
if(s[i]<='9'&&s[i]>='0') b++;
if(s[i]==' ') s++;
}
}
第2个回答  2009-05-03
/*********************************************************
**@author: Lazy_sleeping
**@date: 2009-5-3
**@version: v1.0
**********************************************************/

#include <stdio.h>

void count(char* str)
{
int spaceCount = 0;
int letterCount = 0;
int digitCount = 0;
if (NULL == str)
{
printf("the str is null.");
return;
}
while( '\0' != *str)
{
if (' ' == *str)
spaceCount++;
if ((*str >= 'A' && *str <= 'Z') || (*str >= 'a' && *str <= 'z'))
letterCount++;
if ((*str >= '0') && (*str <= '9'))
digitCount++;
str++;
}
printf("spaceCount is %d\n",spaceCount);
printf("letterCount is %d\n",letterCount);
printf("digitCount is %d\n",digitCount);
}

void main()
{
char* testStr = "f2dk3sj6f7 fkhsdfk88";
count(testStr);
}

相关了解……

你可能感兴趣的内容

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