Created
September 28, 2016 02:22
-
-
Save zironycho/b7d2d32828cbccd903dc1d56b79233af to your computer and use it in GitHub Desktop.
remove white spaces (frontside and backside)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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