#include <stdio.h>
#include <string.h>
void ltrim(char *s) {
char spaces[] = "\n\t\v\f ";
size_t total_spaces_to_trim = strspn(s, spaces);
if (total_spaces_to_trim > 0) {
size_t len = strlen(s);
if (total_spaces_to_trim == len) {
s[0] = '\0';
}
else {
memmove(s, s + total_spaces_to_trim, len + 1 - total_spaces_to_trim);
}
}
}
void rtrim(char *s) {
char spaces[] = "\n\t\v\f ";
int len = strlen(s) - 1;
while (len >= 0 && strchr(spaces, s[len]) != NULL) {
s[len] = '\0';
len--;
}
}
void trim(char *s) {
rtrim(s);
ltrim(s);
}
int main() {
char s[64] = " c python c++ java php ";
printf("'%s'\n", s);
printf("%zu\n", strlen(s));
trim(s);
printf("%zu\n", strlen(s));
printf("'%s'\n", s);
return 0;
}
/*
run:
' c python c++ java php '
28
21
'c python c++ java php'
*/