C/C++ Trimming whitespace - Trim

How to trim strings in C/C++ is a commonly asked question. Below is a simple C/C++ function that will trim leading and trailing whitespaces. Whitespaces include spaces, tabs, carriage return, new lines etc. It get's rid of 'white' spaces at the beginning and at the end of an array of characters.
 1. int trim(char* c){
 2.         char * i;
 3.         if(c==0 || !strlen(c))
 4.                 return 0;
 5.         for(i=c ; isspace(*i)!=0 && (i-c)<strlen(c); i++);
 6.         memmove(c, i, strlen(c)-(i-c)+1);
 7.         if(!strlen(c))
 8.                 return 0;
 9.         for(i=(c+strlen(c)-1) ; isspace(*i)!=0 && i>c; i--);
10.         *(i+1)='\0';
11.         return strlen(c);
12. }
13. 
14. int main(int vv, char** cc){
15.         char  xman[] ="  \t String that needs trimming \n ";
16.         trim(xman);
17.         printf ("--%s--\n", xman);
18. }
Hide line numbers