#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 6
void remove_all_spaces(char *s) {
const char *p = s;
do {
while (*p == ' ') {
p++;
}
} while ((*s++ = *p++));
}
void add_numbers_to_array(char s[], int arr[]) {
char *tmp = strdup(s);
remove_all_spaces(tmp);
int i = 0, num = 0;
char *p = tmp;
while (*p) {
if (isdigit(*p)) {
num = num * 10 + strtol(p, &p, 10);
}
else {
p++;
arr[i++] = num;
num = 0;
}
}
if (isdigit(s[strlen(s) - 1]))
arr[i++] = num;
free(tmp);
}
int main()
{
char s[] = "1, 6472, 7, 9, 12, 899";
int arr[SIZE];
add_numbers_to_array(s, arr);
for (int i = 0; i < SIZE; i++)
printf("%d\n", arr[i]);
return 0;
}
/*
run:
1
6472
7
9
12
899
*/