Created
January 29, 2016 23:21
-
-
Save ifukazoo/1dc923c53b7fbaebc01b to your computer and use it in GitHub Desktop.
trim
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 <ctype.h> | |
#include <stdio.h> | |
#include <string.h> | |
void trim(const char* s, char* buf) | |
{ | |
const char* p = s; | |
while (isspace(*p)) p++; | |
const char* head = p; | |
while (*p) p++; | |
while (isspace(*--p)) ; | |
const char* end = p + 1; | |
strncpy(buf, head, end - head); | |
buf[end - head] = '\0'; | |
return; | |
} | |
int main(void) | |
{ | |
char buf[32]; | |
trim("", buf); printf("[%s]\n", buf); | |
trim("abc", buf); printf("[%s]\n", buf); | |
trim(" abc", buf); printf("[%s]\n", buf); | |
trim(" abc ", buf); printf("[%s]\n", buf); | |
trim(" a bc ", buf); printf("[%s]\n", buf); | |
trim(" abc", buf); printf("[%s]\n", buf); | |
trim(" abc ", buf); printf("[%s]\n", buf); | |
trim(" a bc ", buf); printf("[%s]\n", buf); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment