#include #include /** * Trim whitespace off front and back of string * Assumes a well-formatted (null terminated) string **/ char* trim(char* target) { while (' '==*target || '\t'==*target) target++; char* trailer = target; while ('\0' != *trailer) { trailer++; } if (trailer!=target) { trailer--; while (' '==*trailer || '\t'==*trailer) { *trailer='\0'; trailer--; } } return target; } void trimTest(char* target) { char retar[strlen(target+4)]; strcpy(retar, target); printf("ORIGINAL ||%s||\n", retar); char* ttarget = trim(retar); if (ttarget==retar) { printf("No leading whitespace\n"); } printf("TRIMMED ||%s||\n", ttarget); } int main(void) { trimTest(" this is a test"); trimTest("A second test "); trimTest("\tThird"); }