hello there!
I wanted to write my own implementation of strtok(), which I call my_strtok().. here is my code:
I found out thatCode:char *token; char* my_strtok(char *str, const char *delim) { int i; size_t j; char *string; if (str!=NULL) { string=str; } else { string=token; } if (my_strlen(string)==0) return NULL; int end=0; i=0; while (!end) { if (string[i]=='\0') { token=&string[i]; return NULL; } for (j=0;j<my_strlen(delim);j++) { if (string[i]==delim[j]) { i++; end=0; break; } else { end=1; } } } char *tok; tok=&string[i]; i++; end=0; while (!end) { if (string[i]=='\0') { token=&string[i]; return tok; } for (j=0;j<my_strlen(delim);j++) { if (string[i]==delim[j]) { end=1; break; } } i++; } string[i-1]='\0'; token=&string[i]; return tok; }
causes a segmentation fault... Maybe at the moment I am mentally tired due to a long exercise session, but I cannot find an explaination at a glance...Code:string[i-1]='\0';
What do you think in general of this implementation?
thanks a lot! bye



LinkBack URL
About LinkBacks




