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

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -