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 Vidhan Gupta

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

#include <stdio.h>
#define MAXIMUM 1000
#define MINIMUM 80

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

int main()
{
    int len;
    char line[MAXIMUM];

    while ((len = getLine(line, MAXIMUM)) > 0)
        if (len > MINIMUM)
        {
            printf("%s\n", line);
        }

    return 0;
}

int getLine(char s[], int lim)
{
    int i, c;
    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;
}
INPUT:
This line is not going to be of 80 characters
This line is going to be of 80 characters hence it has to go along with anything I write this is just some words poping in my mind which I'm writing here

OUTPUT:
This line is going to be of 80 characters hence it has to go along with anything I write this is just some words poping in my mind which I'm writing here

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;
}

Solution by arnuld

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

/* 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]$ 

Solution by amarendra

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

#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;
}

Solution by Scopych

Following solution check if the line is greater than 1000 characters.

#include <stdio.h>
#define MAXLINE 1000 /* maximum line length */
#define MINLINE 80 /* minimum line length */

int mygetline(char line[], int maxline);


/* print only lines greater than 80 characters */
int main()
{
    int len;
    char line[MAXLINE];

    while ((len = mygetline (line, MAXLINE)) > 0)
    {
        if ( line[len - 1] != '\n' ) /* check if the line is greater 
than 1000 characters */
        {
            printf ( "the line is greater 1000 characters:\n\n" );
            printf ( "%s\n\n", line );
        }
        else if ( len > MINLINE )
            printf ( "%s\n\n", line );
    }
    return 0;
}



/* mygetline: read a line into s, return length */
int mygetline(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;
}

Solution by lgmlnvsk

The following solution does not contain code snippets from the book.

/* Exercise 1.17 (old 1.14)
"Write a program to print all input lines that are longer than 80 
characters."
Notes:
* "Longer than 80 characters" is equivalent to "81 characters or more".
* There is no directive in the book to use functions in this exercise. 
*/

#include <stdio.h>
#define MINLEN 81 /* minimal length of printed strings
set MINLEN to 1-10 to make program simple to test */

int main()
{
    int ch, pos = 0;            /* ch - input char, pos - char position 
counter */
    char linepart[MINLEN + 1];  /* temporary char buffer for line part 
and \0 */
    while ((ch = getchar()) != EOF)
    {                           /* read input char in ch var until EOF 
*/
        if (pos < MINLEN)       /* if buffer is not full */
            linepart[pos] = ch; /* write char to the current position in
 the buffer */
        else if (pos == MINLEN) /* if buffer is full (line select 
criteria) */
        {                       /* then add \0 and print buffer content 
*/
            linepart[MINLEN] = '\0';
            printf ("%s", linepart);
            putchar(ch);        /* don't forget to print current char */
        }
        else                    /* if line length bigger then buffer 
size*/
            putchar(ch);        /* then print current char */
        if (ch == '\n')         /* if line ends */
            pos = 0;            /* reset current position counter */
        else
            ++pos;              /* else set next position */
    }
    return 0;
}

Solution by CakeOfTrust

Here are some solutions. They all do the same thing but with different limitations.

#include <stdio.h>

#define MAXLINE 10
#define NO 1
#define YES 0
#define PASSVAL 8

int getlines(char s[], lim, char checker[]);

void copyy(char to[], char from[]);

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

    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';

    if (c == EOF)
        checker[0] = YES;

    return i;
}

void copyy(char to[], char from[])
{
    int i = 0;

    while ((to[i] = from[i]) != '\0')
        ++i;
}

int main(void)
{
    int len = 0, templ = len;
    char line[MAXLINE], temp[MAXLINE], eofch[1];

    eofch[0] = NO;

    while (eofch[0] == NO && (len = getlines(line, MAXLINE, 
eofch)) > 0)
    {
        if (len == MAXLINE - 1 && line[MAXLINE - 2] != '\n')
            copyy(temp, line);

        while (eofch[0] == NO &&
              (len * 1.0 / (MAXLINE * 1.0 - 1.0) - len/(MAXLINE - 1)) ==
 0.0 &&
              line[MAXLINE - 2] != '\n' &&
              (templ = getlines(line, MAXLINE, eofch)) != 0)
            len += templ;

        if (len > PASSVAL) {
            if (len <= MAXLINE - 1 && ((len < MAXLINE -1) 
|| line[MAXLINE - 2] == '\n'))
                printf("\n%s", line);

            else
                printf("\n%s", temp);
        }
    }

    return 0;
}

And here are other variants. The ones in comments do things differently.

#include <stdio.h>

#define MAXLINE 10
#define PASSVAL 8
#define NO 1
#define YES 0

int getlines(char *line, int maxline, char ch[]);

int main(void)
{
  int len = 0;
  char line[MAXLINE], eofch[1];

  eofch[0] = NO;

  while (eofch[0] == NO && (len = getlines(line, MAXLINE, 
eofch)) > 0)

      if (len > PASSVAL) {
          printf("\n%s", line);
      }

  return 0;
}

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

  for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i)

      if (i < (lim - 1))
          s[i] = c;


  if (c == '\n') {
      if (i < (lim - 1))
          s[i] = c;

      ++i;
  }

  if (i < lim - 1)
      s[i] = '\0';

  else
      s[lim - 1] = '\0';

  if (c == EOF)
      checker[0] = YES;

  return i;
}

/*
#define MAXTEXT 100

int getlines(char s[], counter);

int getlines(char s[], counter)
{
  int i, c;

  for (i = counter; i < MAXTEXT - 1 && (c = getchar()) != EOF
 && c != '\n'; ++i)
      s[i] = c;

  if (c == '\n') {
      s[i] = c;
      ++i;
  }

  if ((i - counter) < PASSVAL)
      for (; i > counter; --i)
          ;

  s[i] = '\0';

  if (c == EOF)
      return -1;

  else
      return i;
}

getlines(char s[], counter, passval, maxtext);

int getlines(char s[], counter, passval, maxtext)
{
  int i, c;

  for (i = counter; i < maxtext - 1 && (c = getchar()) != EOF
 && c != '\n'; ++i)
      s[i] = c;

  if (c == '\n') {
      s[i] = c;
      ++i;
  }

  if ((i - counter) < passval)
      for (; i > counter; --i)
          ;

  s[i] = '\0';

  if (c == EOF)
      return c;

  else
      return i;
}


int main(void)
{
  int counter;
  char lines[MAXTEXT];

  while ((counter = getlines(lines, counter, PASSVAL, MAXTEXT)) != EOF)
      ;

  printf("%s", lines);

  return 0;
}


int main(void)
{
  int counter;
  char lines[MAXTEXT];

  while ((counter = getlines(lines, counter)) != EOF)
      ;

  printf("%s", lines);

  return 0;
}

*/

Solution by Octavian

#include <stdio.h>

#define MAXLEN 1000

int get_len(char current_line[], int maxline);

int main(void)
{
        int len;
        char ln[MAXLEN];

        while ((len = get_len(ln, MAXLEN)) > 0){
                if (len > 80)
                        printf("%s\n", ln);
                else
                        printf("line < 80 \n");
        }
        return 0;
}

int get_len(char _current_line[], int _maxline)
{
        int i, c;

        for (i = 0; i < _maxline && (c = getchar()) != EOF 
&& c != '\n'; ++i)
                _current_line[i] = c;
        if (c == '\n'){
                _current_line[i] = '\n';
                ++i;
        }
        _current_line[i] = '\0';
        return i;
}

Solution by HanzyBoy

#include <stdlib.h>
#include <stdio.h>

#define ROW 1000
#define WIDTH 5000

char arr[ROW][WIDTH] = {0};               // array to store char strings of sentences - each sentence stored in an individual row
int r = 0;                                // initializing rows
int i = 0;
void printarray(char arr[ROW][WIDTH]);
int lineCount(char arr[ROW][WIDTH], int lineRow);

int main () {
	
	int c;                               // int variable to accept input - will be copied into array
		
		
	while (1) {
	
		c = getchar();
		
		if (c == EOF) {
		
			printarray(arr);
			break;
		}
		
		else if (c == '\n'){
				arr[r][i] = '\0'; 	
				i=0;
				r++;  
			}
			
		else {
			
		arr[r][i] = c;
		i++;
			
		}
	}
		
}

void printarray(char arr[ROW][WIDTH]){
		
		int j;
		int c;
		
		for (j = 0; j < r; j++){ 
		
			c = lineCount(arr, j);
			printf("Count: %d :", c);
			
			if (c >= 80){
			
				printf("%s\n", arr[j]);	
				c=0;
			}
			
			else{
					
					printf("Not 80 characters. \n");
					c=0;
				}
		}

		exit(0);
}


int lineCount(char arr[ROW][WIDTH], int lineRow){
	
	int i;           // counters
	int c;
	
	for(i = 0; c != '\0' ; i++){
		
		c = arr[lineRow][i];
		
		if (c == '\0')
			return i;
	}
	}
Personal tools