I was taking a look at the 2 C-string functions, strtok_r() and strsep(), and noticed both functions modify the location of the original string passed in.
Are there any other C-string functions that don't modify the original string passed in?
In my application, the original string is dynamically allocated, so I wish to free the original string after the parsing is done.
An example with strtok_r()
int main(){
char * str = strdup("Tutorial and example");
char* token;
char* rest = str;
printf("%s\n", rest);
while ((token = strtok_r(rest, " ", &rest)))
printf("%s\n", token);
printf("\n%s\n",str);
return(0);
}
Output
Tutorial and example
Tutorial
and
example
Tutorial
In the very last line, I wish for str to point to the unmodified cstring "Tutorial and example".
A similar output would have occured with strsep() as well.
int main(){
char * str = strdup("Tutorial and example");
char* token;
char* rest = str;
printf("%s\n", rest);
while ((token = strsep(&rest, " ")))
printf("%s\n", token);
if (rest != NULL)
printf("%s\n", rest);
printf("%s\n", str);
return(0);
}
Thank you.