#include <stdio.h>
#include <string.h>
void trim_right(char *s) {
char *space_pos = NULL;
for(;;++s) {
char ch = *s;
if(ch == '\0') {
break;
} else if(ch == ' ') {
if(!space_pos)
space_pos = s;
} else {
space_pos = NULL;
}
}
if(space_pos) {
*space_pos = '\0';
}
}
char buf[1024];
void test(const char *arg, const char *expected) {
trim_right(buf);
if(strcmp(buf
, expected
) == 0) { printf("OK: '%s' -> '%s'\n", arg
, expected
); } else {
printf("FAIL: arg is '%s', expected '%s', found '%s'.\n", arg
, expected
, buf
); }
}
int main(void) {
test("", "");
test("word", "word");
test(" foo ", " foo");
test(" bar ", " bar");
test(" ", "");
test("this failure is expected", "...");
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdHJpbmcuaD4KCnZvaWQgdHJpbV9yaWdodChjaGFyICpzKSB7CgljaGFyICpzcGFjZV9wb3MgPSBOVUxMOwoJZm9yKDs7KytzKSB7CgkJY2hhciBjaCA9ICpzOwoJCWlmKGNoID09ICdcMCcpIHsKCQkJYnJlYWs7CgkJfSBlbHNlIGlmKGNoID09ICcgJykgewoJCSAgICBpZighc3BhY2VfcG9zKQogICAgICAgICAgICAgICAgc3BhY2VfcG9zID0gczsKCQl9IGVsc2UgewoJCQlzcGFjZV9wb3MgPSBOVUxMOwoJCX0KCX0KCWlmKHNwYWNlX3BvcykgewoJCSpzcGFjZV9wb3MgPSAnXDAnOwoJfQp9CgpjaGFyIGJ1ZlsxMDI0XTsKdm9pZCB0ZXN0KGNvbnN0IGNoYXIgKmFyZywgY29uc3QgY2hhciAqZXhwZWN0ZWQpIHsKCXNucHJpbnRmKGJ1Ziwgc2l6ZW9mKGJ1ZiksICIlcyIsIGFyZyk7Cgl0cmltX3JpZ2h0KGJ1Zik7CglpZihzdHJjbXAoYnVmLCBleHBlY3RlZCkgPT0gMCkgewoJCXByaW50ZigiT0s6ICclcycgLT4gJyVzJ1xuIiwgYXJnLCBleHBlY3RlZCk7Cgl9IGVsc2UgewoJ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