10.16输入一个字符串,内有数字和非数字字符,如:
a123x456 17960? 302tab5876
将其中连续的数字作为一个整数,依次存放到一数组num中。例如123放在num[0]中,456放在num[1]中……统计共有多少个整数,并输出这些数。
#include <stdio.h>
#include <string.h>
#include <math.h>
int output(char* str, int* a)
{
int n = strlen(str);
int temp[50];
int sum=0, j=0, k,p;
int m = 0;
int num = 0;
char* i;
for (i = str; i < str + n + 1; i++)
{
if (*i >= '0' && *i <= '9')
{
num = 1;
temp[j++] = (int)(*i - '0');
}
else if (num)
{
sum = 0;
num = 0;
p = j - 1;
for (k = 0; k < j; k++)
{
sum += temp[k] * pow(10, p);
p--;
}
a[m++] = sum;
j = 0;
}
}
return m;
}
int main()
{
char str[50];
int a[50];
gets(str);
int m=output(str,a);
for (int i = 0; i < m; i++)
printf("%d ",a[i]);
return 0;
}
标签:提取,数字,int,char,num,str,字符串,include
From: https://blog.csdn.net/m0_53374272/article/details/136989195