If you're just trying to remove whitespace, the idea is to attach two pointers to the
string, one is the iterator, the other the copier. Only increment the copyier if the
undesired char isn't present. Null terminate when done. If you plot it out on a piece
of paper, it will make more sense.
Code:
char * stremove(char * string, char remove)
{
char * iter, * copy = string;
for(iter = string; *iter != 0; ++iter)
if(*iter != remove)
*(copy++) = *iter;
*copy = 0;
return string;
}
Which is semantically equivalent to:
char * stremove(char * string, char remove)
{
char * iter, * copy = string;
int i, j = 0;
for(i = 0, iter = string; iter[i] != 0; ++i)
if(iter[i] != remove)
copy[j++] = iter[i];
copy[j] = 0;
return string;
}
Then it's a simple matter of code reuse:
char * eatwhite(char * string)
{
return stremove(string, ' ');
}