1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 char *full_trim(const char *); 6 7 int main (void) 8 { 9 char str[] = " Hello world!"; 10 char *dst; 11 12 printf("%s\nlen %ld\n", str, strlen(str)); 13 dst = full_trim(str); 14 printf("%s\nlen %ld\n", dst, strlen(dst)); 15 16 return 0; 17 } 18 19 char *full_trim(const char *src) 20 { 21 char *dst; 22 size_t i, j, len; 23 24 len = strlen(src); 25 26 /* Counts starting blank spaces */ 27 for(i = 0; src[i] == ' '; i++); 28 29 /* Counts ending blank spaces */ 30 for(j = len; src[j - 1] == ' '; j--); 31 32 if(!(dst = (char *)calloc(j - i, sizeof(char)))) 33 return NULL; 34 35 strcpy(dst, src + i); 36 dst[j - i] = '\0'; 37 return dst; 38 }