c语言统计将已知字符串中数字符的个数;请纠正程序中存在错误,

来源:百度知道 编辑:UC知道 时间:2024/05/24 16:04:01
#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));
}

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

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