您的位置:首页 > 其它

将数字从《字符串》中提取出来

2015-05-27 17:57 295 查看
#include "stdio.h"
void getnum(char *in,char *out)
{
char *t = in;
char *p = out;

while(*t != '\0')
{
if(*t >= '0'&& *t <= '9')
*p++ = *t++;
else t++;
}
*p = '\0';
}

void main()
{   int i =0;
char * s = (char *)malloc(sizeof(char));
char * p = (char *)malloc(sizeof(char));
gets(s);
getnum(s,p);
printf("%s\n",p);
printf("%s",s);
}
//******************************方法二************************

#include "stdio.h"
void main()
{   int i =0;
char * s = (char *)malloc(sizeof(char));
char * p = (char *)malloc(sizeof(char));
gets(s);
while(*s != '\0')
{
if(*s >= '0'&& *s <= '9')
{*p++ = *s++;
i++;
}

else s++;
}
*p = '\0';
printf("%s\n",p-i);//要把移位的位数减去
printf("%s",s);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  字符串