-
sscanf
i know you can split strings using strtok..
but i was wondering if it was possible using sscanf..
but the trick is that i dont know how many tokens there will be in the string..
eg str = "hi there this is cool"
and i want it to print
hi
there
this
is
cool
i've tried a while loop but it gets stuck on the first word infinetley
hi
hi
hi
....
??? any ideas?
-
Do you need to store the strings or just print them out ? If just printing them out you dont really need to use sscanf.
How about something like this :
Code:
#include <stdio.h>
void split_up(char *string, char delimiter)
{
for( ; *string != '\0' ; string++ )
{
if( *string == delimiter ) printf("\n");
else printf("%c", *string);
}
}
int main(void)
{
char string[] = "This is a test";
split_up(string, ' ');
return 0;
}
Does this help ?
Stan
-
Use the "%n" conversion to tell you how many characters have been processed
Something like
Code:
char *p = str;
int n;
while ( sscanf( p, "%s%n", word, &n ) == 1 ) {
printf( "word=%s\n", word );
p += n;
}
Note: %n does NOT increment the return result of sscanf()