Jump to: navigation, search

The C Programming Language, 2nd Edition, by Kernighan and Ritchie
Exercise 1.17 on page 31

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

Solution by MJSR

#include <stdio.h>

#define MINLENGTH 81

int readbuff(char *buffer) {
    size_t i=0;
    int c;
    while (i < MINLENGTH) {
        c = getchar();
        if (c == EOF) return -1;
        if (c == '\n') return 0;
        buffer[i++] = c;
    }
    return 1;
}

int copyline(char *buffer) {
    size_t i;
    int c;
    int status = 1;
    for(i=0; i<MINLENGTH; i++)
        putchar(buffer[i]);
    while(status == 1) {
        c = getchar();
        if (c == EOF)
            status = -1;
        else if (c == '\n')
            status = 0;
        else
            putchar(c);
    }
    putchar('\n');
    return status;
}

int main(void) {
    char buffer[MINLENGTH];
    int status = 0;
    while (status != -1) {
        status = readbuff(buffer);
        if (status == 1)
            status = copyline(buffer);
    }
    return 0;
}



a simple solution, just a modification of K&R2's example in section 1.9.

Solution by arnuld

/* K&R2: 1.9, Character Arrays, exercise 1.17

STATEMENT:
write a programme to print all the input lines
longer thans 80 characters. 


*/


#include<stdio.h>

#define MAXLINE 1000
#define MAXLENGTH 81

int getline(char [], int max);
void copy(char from[], char to[]);

int main()
{
  int len = 0; /* current line length */
  char line[MAXLINE]; /* current input line */

  while((len = getline(line, MAXLINE)) > 0)
    {
      if(len > MAXLENGTH)
	printf("LINE-CONTENTS:  %s\n", line);
    }

  return 0;
}



int getline(char line[], int max)
{
  int i = 0; 
  int c = 0; 

  for(i = 0; ((c = getchar()) != EOF) && c != '\n' && i < max - 1; ++i)
    line[i] = c;

  if(c == '\n')
    line[i++] = c;

  line[i] = '\0';

  return i;
}



this is a sample RUN

[arch@voodo kr2]$ gcc -ansi -pedantic -Wall -Wextra -O ex_1-17.c
[arch@voodo kr2]$ ./a.out 
like htis
and
this line has more than 80 characters in it so it will get printed on the terminal right now without any troubles. you can see for yourself
LINE-CONTENTS:  this line has more than 80 characters in it so it will get printed on the terminal right now without any troubles. you can see for yourself

but this will not  get printed
[arch@voodo kr2]$ 


Following solution does not impose any maximum limit on the line. We continue printing until we reach EOF or newline.
Solution by amarendra

#include <stdio.h>

#define MINLEN  81

int
getline(char s[], int max);

int
main(void)
{
    int len, c, i;
    char line[MINLEN];

    while ((len = getline(line, MINLEN)) != 0) {
        if ((len == MINLEN-1) && (line[len-1] != '\n')) {
            printf("%s", line);
            while ((c = getchar()) != EOF && c != '\n')
                putchar(c);
            putchar('\n');
        }
    }

    return 0;
}

int
getline(char s[], int max) {
    int i, c;
    for (i=0; i<max-1 && (c=getchar())!=EOF && c!='\n'; ++i) {
        s[i] = c;
    }
    if (c == '\n') {
        s[i] = c;
        i++;
    }
    s[i] = '\0';
    return i;
}
Personal tools
Personal tools
Tidy_icons
not logged in