First of all take into account that though the new line character
'\n' is a white space character it shall not be deleted from a string even if it is a trailing character in a string. So you may not use function
isspace. Instead you should apply standard function
isblank if your compiler supports it. Otherwise you have to compare characters of the string with blank and tab characters yourself.
Here is a possible realization of the function within the demonstrative program
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char * trim( char *s )
{
char *p = s;
while ( isblank( *p ) ) ++p;
char *q = p + strlen( p );
while ( q != p && isblank( *( q - 1 ) ) ) --q;
memcpy( s, p, q - p );
s[q - p] = '\0';
return s;
}
int main(void)
{
char s[] = " Hello lordcideon! ";
printf( "\"%s\"\n", s );
printf( "\"%s\"\n", trim( s ) );
return 0;
}
The output will be
" Hello lordcideon! "
"Hello lordcideon!"
If your compiler does not support function
isblank then you should write explicitly for example
while ( *p == ' ' || *p == '\t' ) ++p;
instead of
while ( isblank( *p ) ) ++p;