K&R Solutions - 1.17

Q: Write a program to print all input lines that are longer than 80 characters.

A:

/* This is really a simpler version of the longest-line program: that already
 * has most of what we need. */
#include <stdio.h>
#define MAXLINE 1000

int get_a_line(char s[], int lim);


int main(void)
{
  int len;
  char line[MAXLINE];

  while ((len = get_a_line(line, MAXLINE)) > 0) {
    if (len > 80) {
      printf("%s", line);
    }
  }

  return 0;
}

int get_a_line(char s[], int lim) 
{
  int c, i;

  for (i = 0; i < lim-1 && (c=getchar()) != EOF && c != '\n'; ++i) {
    s[i] = c;
  }
  if (c == '\n') {
    s[i] = c;
    ++i;
  }
  s[i] = '\0';
  
  return i;
}
This is a short sentence, which will not be repeated.
This, however, is a very, very, very long sentence, which will be repeated back by the program.     
This, however, is a very, very, very long sentence, which will be repeated back by the program.