Home > Software engineering >  String tokenization terminates before reading the entire string
String tokenization terminates before reading the entire string

Time:12-13

I've been trying to split a string by whitespace character. The format of the string is a, b and I need to split it as a and b. The code I've written is as below but the loop always terminates before printing the last character.

void tokenize(char *str)
{
    int i = 0, k = 0;
    char *ch = malloc(1000);
    printf("%s\n", str);
    while(i < strlen(str))
    {
        if(str[i] == ' ')
        {
            k = 0;
            printf("%s", ch);
            ch = malloc(1000);
            i  ;
        }
        else if(str[i] == ',')
            i  ;
        else
        {
            ch[k  ] = str[i  ];
        }
    }
}

Output

q1, q2
q1   

As it's evident from the output above, it terminates before printing q2, a solution to solve this issue is appreciated.

CodePudding user response:

You need to print after the loop as the print is only done when reading a space:

#define STRLEN 1024

void tokenize(const char *str)
{
    int i = 0, k = 0;
    free(ch);
    char *ch = malloc(STRLEN);
    printf("%s\n", str);
    while(i < strlen(str))
    {
        if(str[i] == ' ')
        {
            k = 0;
            printf("%s\n", ch);
            ch = malloc(STRLEN);
            i  ;
        }
        else if(str[i] == ',')
            i  ;
        else
        {
            ch[k  ] = str[i  ];
        }
    }
    printf("%s\n", ch);
    free(ch);
}
  • Related