Hi
This code, compiles fine but results in a segmentation fault and when I comment out the part which adds the null character then the segmentation fault doesn't appear and it removes whitespace from the beginning but not the end.
#include <ctype.h>
#include <string.h>
#include <stdio.h>
char *trim(char *s)
{
char *end;
while(isspace(*s))
{
s++;
}
if(*s == 0) // All spaces?
{
return s;
}
end = s + strlen(s) - 1;
while(end > s && isspace(*end))
{
end--;
}
// Write new null terminator
//*(end+1) = 0;
return s;
}
int main (int argc, char* argv[])
{
char *s = " Hello world ";
s = trim(s);
printf("%s", s);
}