统计字符串中数字符的个数

来源:百度知道 编辑:UC知道 时间:2024/09/22 12:43:29
rt 改错的
#include<stdio.h>
int digits(char *s)
{int c=0;
while(s)
{
if(*s >=0&&*s <=9)
c++;
s++;
}
return c;
}
void main()
{
char s[80];
printf("请输入一行字符\n");
gets(s);
printf("字符长度是:%d\n",digits(s));
}

错在这里:
if(*s >=0&&*s <=9)
应该是:
if(*s >='0'&&*s <='9')

#include<stdio.h>
int digits(char *s)
{int c=0;
while(s) //while (*s)
{
if(*s >=0&&*s <=9) //if(*s>='0'&&*s<='9'
c++;
s++;
}
return c;
}
void main()
{
char s[80];
printf("请输入一行字符\n");
gets(s);
printf("字符长度是:%d\n",digits(s));
}