I have to read the arguments that I introduce when I run a program on linux.
./myprog 10 20 30, 20 54 12, 31 42 51
I have a problem finding out how to separate the arguments into a substring and then that substring in other string.
10 20 30, 20 54 12, 31 42 51
I want to separate this string into another string with "," being the separator and then that substring to separate into another string with " " being the separator.
a[0]="10 20 30"
a[1]="20 55 12"
a[2]="31 42 51"
Then I want it to be like that:
b[0]="10" b[1]="20" b[2]="30" and so on...
Here I make this code to separate the arguments into a substring.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *str = "10 20 30, 20 54 12, 31 42 51";
char **a = calloc(sizeof(char*),strlen(str));
int x = 0;
int y = 0;
int i = 0;
while (str[i] != '\0')
{
x = 0;
a[y] = calloc(sizeof(char),9);
while(str[i] != ',' && str[i] != '\0')
{
a[y][x] = str[i];
i++;
x++;
}
y++;
i += 2;
}
//this code below for test
y--;
for (int t = 0; t < y; t++)
printf("%s\n",a[t]);
}
now try to make the other one :).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.