首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C语言习题,求指导,该如何解决

2012-03-23 
C语言习题,求指导下列程序的功能为:统计已知字符串中数字符的个数;请纠正程序中存在错误,使程序实现其功能

C语言习题,求指导
下列程序的功能为:统计已知字符串中数字符的个数;请纠正程序中存在错误,使程序实现其功能,程序以文件名sy8_2.c存盘。
#include<stdio.h>
int digitCount(char *s)
{
int count=0;
while(s)
{
if(*s >=0&&*s <=9)
count++; 
s++;
}
return count;
}
void main()
{
char s[80];
printf("请输入一行字符\n");
gets(s);
printf("字符串中数字字符个数是:%d\n",digitCount(s));
}


[解决办法]

C/C++ code
#include<stdio.h>int digitCount(char *s){    int count=0;    while(*s)       //是否是字符串末尾    {        if(*s >= '0' && *s <= '9') //字符比较            count++;          s++;    }    return count;}void main(){    char s[80];    printf("请输入一行字符\n");    gets(s);    printf("字符串中数字字符个数是:%d\n",digitCount(s));} 

热点排行