1

In the following program, I try to cut the string into individual strings. In the function I used strtok, where I have an adjustable space as a delimiter, but its not work for everything (\t). Is it possible to set more delimiters? Or choose a completely different way of dividing words?

int listOneDay(const char *src)
{
    char str[100000];
    strcpy(str,src);
    
    char **res = NULL;
    char *p = strtok(str, " ");
    int n_spaces =0;
    int i;
    
    while (p)
    {
        
        res = realloc(res, sizeof(char*)* ++n_spaces);
        
        if (res == NULL)
            exit(-1);
        
        res[n_spaces-1]=p;
        p=strtok (NULL," ");
    }   
    
    res = realloc(res,sizeof(char*)*(n_spaces+1));
    res[n_spaces]=0;
    
    for(i=0; i<(n_spaces) ; ++i)
        printf("%s\n",res[i]);
    
    free(res);

    return 1;
}      

int main ( void )
{ 
    const char * str1 =
    "2021-01-01 2021-7-1 \'Face masks\' \"Wear everywhere\"\t1/5/2021 \'March 5, 2021\' \"Face Masks\" \'Wear almost everywhere\'\n"
    "1/5/2021 \'March 5, 2021\' \"Face Masks\" \'Wear almost everywhere!\'\n"
    "10.1.2021 \'5.2.2021\' Lockdown \"Complete\"\n"
    "\'jan 2 2021\' \"JANUARY 15 2021\" lockdown \'Comlpete except dogwalking\'\n"
    "2021-1-20 2021-01-20 LOCKDOWN \'Between 9PM and 6AM\'\n";
    listOneDay(str1);
  
  return 0;
}

0 Answers0