I have an string that have an extra spaces string, for example:
char * s = " foo baa ";
I want to conver it to:
foo baa
I have wrote this function:
void trim (char ** src)
{
char * p = strdup(* src);
char * ret = malloc(strlen(*src) + 1);
assert(ret != NULL);
char * token;
token = strtok(p, " \t");
while( NULL != token ) {
while (*token) {
*(ret ++) = *(token ++);
}
token = strtok(NULL, " \t");
}
printf("ret = %s\n", ret);
}
but it given for me an empty string from ret variable value. someone may point out my mistake? thanks in advance.
You are incrementing
retin your while, store the original address or use subscript to access different chars ofret.