改错:把字符串里的数字转化数字

来源:百度知道 编辑:UC知道 时间:2024/06/07 20:33:30
下面的代码是想把字符串里面的数字符号取出来,比如"aA12345bc78hk",就打印12345,不要后面的78(因为中间间隔由字母不连续了),写了个代码,不知错在哪里,请大侠们帮找下错。
#include<stdio.h>
#include<string.h>
int isdigit(char c)
{
return((c>='0')&&(c<='0'+9))?1:0;
}
main()
{
int i=0;
char str[]="aA12345bc78hk";
char *s=str;
char digit[strlen(str)];
whlie(*s){
if(isdigit(*s)){
digit[i++]=*s;
s++; //这里自增是为了判断下一个字符还是不是数字。
if(!isdigit(*s)){
digit[i]='\0';
break; //如果再是数字了,跳出循环。
}
s--; //让指针往回跳一步。
}
s++;
}
return 0;
}
不好意思,两个地方手误敲错了:
1、不要后面的78(因为中间间隔有字母不连续了),
2、break; //如果不再是数字了,跳出循环。

我写了个,不知符合要求吗。
#include <stdio.h>
#include<string.h>
int isdigit(char c)
{
return((c>='0')&&(c<='0'+9))?1:0;
}
int main()
{
char str[]="aA12345bc78hk";
char digit[20]={0};
char *s=str;
int i=0;
while(!isdigit(*s))
s++;
while(isdigit(*s))
digit[i++]=*(s++);
printf("%s",digit);
return 0;
}