输入一个字符串,内有数字和非数字字符,将其中连续的数字作为一个整数,依次存放到一数组a中。统计共有多少个整数,并输出这些数。

 
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

int main(){
    setvbuf(stdout,NULL,_IONBF,0);
    char s[255];
    int a[255];            //存放得到的整数
    int i,length;
    int f(char *s,int *a);

    printf("Input the string:");
    gets(s);
    length=f(s,a);
    printf("There are %d numbers.
",length);
    printf("They are:");
    for(i=0;i<length;i++)
        printf("%d ",a[i]);

    return EXIT_SUCCESS;
}

int f(char *s,int *a){
    char *p;
    int t[255];                //存放一个整数的每位数
    int len=strlen(s);
    int flag=0;                //标记当前字符是否为0-9
    int i=0,j;
    int num=0;                //整数的个数
    for(p=s;p<=s+len;p++)     //把最后的字符也当作非数字字符处理
    {
        if(*p>='0'&&*p<='9')
        {
            flag=1;
            t[i++]=*p-'0';    //把字符'0'~'9'装换为数字0~9
        }
        else if(flag==1)     //如果当前字符非数字字符,且前一个字符为数字字符时,开始记录整数
        {
            a[num]=0;
            for(j=0;j<i;j++)
                a[num]=a[num]+t[j]*pow(10,i-1-j);

            num++;
            flag=0;
            i=0;
        }
    }
    return num;
}
//如果使用VC,去掉第7行。
原文地址:https://www.cnblogs.com/Camilo/p/3371550.html