我写了一个类似的#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **get_string(char *str)
{
char buf[20];
int index = 0;
char **ret = NULL;
int ret_point_count = 0;
while(1)
{
while(*str <= '0' || *str >= '9')
{
if(*str == '\0') goto e;
str++;
}
index = 0;
while(*str >= '0' && *str <= '9') buf[index++] = *str++;
buf[index++] = '\0';
ret_point_count++;
ret = realloc(ret, sizeof(char **) * ret_point_count);
ret[ret_point_count - 1] = malloc(strlen(buf) + 1);
strcpy(ret[ret_point_count - 1], buf);
}
e:
ret_point_count++;
ret = realloc(ret, sizeof(char **) * ret_point_count);
ret[ret_point_count - 1] = NULL;
return ret;
}
int main(void)
{
char s[50];
char **str_arr;
fgets(s, 50, stdin);
str_arr = get_string(s);
for(int i = 0; str_arr[i] != NULL; i++)
{
puts(str_arr[i]);
}
return 0;
}
123qwer4567sdfg567rtyufgh6789tyu5678
123
4567
567
6789
5678
请按任意键继续. . .
|