Strip a Character from the End of a String
Overwrite trailing CR, LF, and a chosen character with NULs so the string no longer ends on those bytes.
#include <string.h>
char *stripend(char *str, char c)
{
char *ptr;
if (str == NULL || *str == '\0') {
return str;
}
ptr = strchr(str, '\0');
ptr--;
while (ptr >= str && (*ptr == '\n' || *ptr == '\r' || *ptr == c)) {
*ptr-- = '\0';
}
return str;
}
Caveats
Mutates the buffer in place. Empty and NULL strings are returned unchanged. Needs string.h.