I have to write a function for my program on trimming a string. This is what I have to work with.
char *str_trim(char *);
This function will remove all space (' ') and tab ('\t') characters from the passed in string argument. The newly formatted string should be returned via the passed in string argument.
For example, if the passed in string argument contains "Hello World Again", this function will change it to "HelloWorldAgain".
Does anyone know how to help me on this one? Thanks
Setting aside the fact that there are a zillion easier ways to do it...
I'm guessing you need to do this for a programming class, which means I'm not going to write the function for you, but I will help you with the algorithm:
Have two pointers. Start both of them at the beginning of the input string. One pointer will be a "read" pointer from which you'll read the next character in the input string. The other pointer will be a "write" pointer to write you write the character you just read as long as it's not a space or a tab.
Continue to loop, reading the next character and possibly writing it, until you find the NULL terminator. Then, exit the loop, and don't forget to write a NULL terminator via your "write" pointer so the new string is terminated correctly.