#include <stdio.h>
int Number(char );
void fun(char *, int *, int *);
void fun(char *str, int *a, int *n )
{
int j, k = 0, temp, len = 0, flag = 0;
char *p, *q = str;
j = 0;
p = str;
while(*p != '\0')
{
if(!Number(*p))
{
flag = 1;
}
else
{
if(flag)
{
j++;
flag = 0;
}
else j++;
}
if(flag && j != 0)
{
if(j == 1)
{
a[len++] = *(p-1) - '0';
j = 0;/*没有归零导致后面进入了莫名其妙的数字*/
}
else
{
/*
q = p-1;
temp = 0;
for(;j>0;j--)
{
temp = temp + (*q - '0');
q--;
temp = temp *10;
}
a[len++] = temp;
不可以这样写,把数字给颠倒了。*/
q = p-j;
temp = 0;
for(;j>0;j--)
{
temp = temp + (*q - '0');
q++;
temp = temp * 10;
}
a[len++] = temp/10;
}
}
p++;
}
if( j != 0)
{
if(j == 1)
{
a[len++] = *(p-1) - '0';
j = 0;
}
else
{
q = p-j;
temp = 0;
for(;j>1;j--)
{
temp = temp + (*q - '0');
q++;
temp = temp * 10;
}
a[len++] = temp/10;
}
}
*n = len;
}
int Number(char a)
{
if(a >= '0' && a<= '9')
return 1;
else return 0;
}
int main()
{
char s[100] = {"he78!8j?3"};//测试数据
int a[10], len, i;
fun(s, a, &len);
for(i = 0; i <len; i++)
printf("%d\n", a[i]);
return 0;
}
运行结果: