I am trying to trim the end of a ANSI C string but it keeps seg faulting on the trim
function.
#include <stdio.h>
#include <string.h>
void trim (char *s)
{
int i;
while (isspace (*s)) s++; // skip left side white spaces
for (i = strlen (s) - 1; (isspace (s[i])); i--) ; // skip right side white spaces
s[i + 1] = '\0';
printf ("%s\n", s);
}
int main(void) {
char *str = "Hello World ";
printf(trim(str));
}
I can't seem to figure out why. I've tried like 15 different trim
functions they are all seg faulting.