arrays - Implementing Split function in C -
i'm trying write c function similar split
in java. when @ main, instead of add function, works perfectly. couldn't understand why not work add function.
#include <stdio.h> #include <string.h> char *words[50] = { null }; void add(const char *word) { static int = 0; words[i] = word; i++; } int main( void ) { char string[65]; char *tokenptr; fgets(string, 65, stdin); tokenptr = strtok( string, " " ); add(tokenptr); int = 0; while ( tokenptr != null ) { add(tokenptr); tokenptr = strtok( null, " " ); } int i; for(i = 0; words[i] != null; i++) puts(words[i]); return 0; }
this little piece of actual code, there reasons why need in function.
remove first add
calling.
fgets(string, 65, stdin); tokenptr = strtok( string, " " ); // add(tokenptr); // remove
since you'll add first token in next while
loop.
also, should remove duplication of int i
.
// int i; // <-- why there? for(i = 0; words[i] != null; i++) puts(words[i]);
Comments
Post a Comment