Hallo Lolcoman!
May be you can use strtok() declared in string.h (also cstring)
you can see a example:
Code:
// splits a String into several parts
#include <stdio.h> //printf
#include <stdlib.h> //atoi EXIT_SUCCESS
#include <string.h> // strcpy, strtok
// argument 1(*var)= strint to split
// argument 2=delimiter
// argument 2 = 2 dimensional array where the tokens are copied
// Return-Value: number of tokens where found
int mixed(char *var, char *delimiter, char numbers[20][30])
{
printf ("the delimiter is: %s(space)\nTokens:\n", delimiter);
int retval = 0, i = 0;
char * d;
d = strtok (var, delimiter);
while (d != NULL)
{
strcpy(numbers[i], d);
printf ("i: %2d String: %s\n", i, numbers[i]);
i++;
d = strtok (NULL, delimiter);
retval++;
}
return retval;
}
int main(int argc, char **argv)
{
char test[] = {"60 10 Winston Charly John 44 67 Henry 0 Mary-Jane_Lovley"};
char delimiter[] = " ";
char numbers[20][30] = {0};
int ival = 0, i,j, result_a, sum = 0;
printf ("String to split into tokens:\n%s\n\n", test);
result_a =mixed(test, delimiter, numbers);
printf ("\nNumber of tokens: %d\n-----tokens splited into characters-------\n", result_a);
for (i = 0; i < result_a; i++)
{
printf("i: %2d ", i);
for (j = 0; j < 30; j++)
printf(" %c ", numbers[i][j]);
printf("\n");
}
printf ("-----tokens converted into int-values------\n");
for (i = 0; i < result_a; i++)
{
ival = atoi(numbers[i]);
sum += ival;
printf("ival: %d\n", ival);
}
printf("\nsum of all int-values: %d\n\n", sum);
return EXIT_SUCCESS;
}
This code is not without failures because the argument number 3,
the char array must have enough size if you don't want to have a
overflow.
output of the example:
Code:
String to split into tokens:
60 10 Winston Charly John 44 67 Henry 0 Mary-Jane_Lovley
the delimiter is: (space)
Tokens:
i: 0 String: 60
i: 1 String: 10
i: 2 String: Winston
i: 3 String: Charly
i: 4 String: John
i: 5 String: 44
i: 6 String: 67
i: 7 String: Henry
i: 8 String: 0
i: 9 String: Mary-Jane_Lovley
Number of tokens: 10
-----tokens splited into characters-------
i: 0 6 0
i: 1 1 0
i: 2 W i n s t o n
i: 3 C h a r l y
i: 4 J o h n
i: 5 4 4
i: 6 6 7
i: 7 H e n r y
i: 8 0
i: 9 M a r y - J a n e _ L o v l e y
-----tokens converted into int-values------
ival: 60
ival: 10
ival: 0
ival: 0
ival: 0
ival: 44
ival: 67
ival: 0
ival: 0
ival: 0
sum of all int-values: 181
If you would change some things in 'mixed' you could
save directly the converted int-values into a int-array.
But that will be your work.
Sorry but my English is not very good, so i prefer to take examples.