In char* str = "Hello world "
, the string "Hello World "
is stored in the read only of the address space. Trying to modify the read only memory causes undefined behavior.
Instead use
char str[] = "Hello World ";
or
char *str = malloc(sizeof("Hello World ")+1);
strcpy(str, "Hello World ");
and then try trim functions...
For more info on how memory is allocated for variables, refer to https://stackoverflow.com/a/18479996/1323404