Skip to content

Instantly share code, notes, and snippets.

@zironycho
Created September 28, 2016 02:22
Show Gist options
  • Save zironycho/b7d2d32828cbccd903dc1d56b79233af to your computer and use it in GitHub Desktop.
Save zironycho/b7d2d32828cbccd903dc1d56b79233af to your computer and use it in GitHub Desktop.
remove white spaces (frontside and backside)
#include <stdio.h>
#include <ctype.h>
#include <string.h>
static void strtrim(char *inout)
{
char *start = inout;
char *end = inout + strlen(inout) - 1;
int i;
while (isspace(*start)) { start++; }
while (isspace(*end) && (end-start) > 0) { *end = '\0'; end--; }
if (start != inout) {
strcpy(inout, start);
}
}
int main()
{
char str[][10] = {
" hello",
"hello ",
" hello",
"hello ",
" ",
"",
};
for (int i = 0; i < 6; i++) {
printf("[%s] > ", str[i]);
strtrim(str[i]);
printf(" > [%s]\n", str[i]);
}
return 0;
}
/*
output
-----------------------------------
[ hello] > > [hello]
[hello ] > > [hello]
[ hello] > > [hello]
[hello ] > > [hello]
[ ] > > []
[] > > []
-----------------------------------
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment