Skip to content

Instantly share code, notes, and snippets.

@codebrainz
Created April 9, 2012 04:15
Show Gist options
  • Select an option

  • Save codebrainz/2341387 to your computer and use it in GitHub Desktop.

Select an option

Save codebrainz/2341387 to your computer and use it in GitHub Desktop.
Convert newlines to \n and strip extra trailing whitespace
#include <stdio.h>
#include <string.h>
#define MAXLINE 16484
int main(void)
{
int c;
size_t len;
char line[MAXLINE] = { 0 };
while (NULL != fgets(line, MAXLINE, stdin))
{
if ('\0' != line[0])
{
len = strlen(line);
while (len--)
{
c = line[len];
if (c == ' ' || c == '\t' || c == '\n' || c == '\r')
line[len] = '\0';
else
break;
}
}
fputs(line, stdout);
fputc('\n', stdout);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment