I have a string such as "first second" and my desired result is for this output:
first
second
But the output I am getting is:
first
first second
I know there is a problem either in my update statements or when I create a substring. If anybody could help me out that would be great. Here is my code down below:
int counter = 0; //counter used in loop
int index = test->current_index; //holds the current index of my string, it's initially 0
char *string = test->myString; //holds the whole string
char token_buffer = string[index];
//before loop: index = 0, counter = 0
while(test->current_index <= test->end_index) //test->end_index holds last index of string
{
while(token_buffer != ' ')
{
counter++;
token_buffer = string[index + counter];
}
char *output_token = malloc(counter+1);
strncpy( output_token, string, counter );
//printing token
printf("%s \n", output_token);
//update loop (possible problem area!)
test->current_index += counter;
index += counter;
token_buffer+=string[counter];
counter =0;
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire