|||

Learn To Solve It

Quick search

  • A Tutorial Introduction
    • 1.1 Getting Started
    • 1.1 testing hello, world
    • 1.2 Variables and Arithmetic Expressions
    • 1.3 The for statement
    • 1.3 Temperature Convertor
    • 1.4 Symbolic Constants
    • 1.4 Temperature Convertor
    • 1.5.1 File Copying
    • 1.5.2 Character Counting
    • 1.5.2 Character Counting2
    • 1.5.3 Line Counting
    • 1.5.4 Word Counting
    • 1.5 Character Input and Output
    • 1.5 Temperature Convertor in Reverse
    • 1.6 Arrays
    • 1.6 Verify the value of EOF
    • 1.7 Functions
    • 1.7 Value of EOF
    • 1.8 Count blanks, tabs and newlines
    • 1.9 Character Arrays
    • 1.9 Replace Continous blanks with a single blank
    • 1.10 External Variables and Scope
    • 1.10 Explicit Tabs, Backslash and Backspaces
    • 1.11 Test Word count program
    • 1.13.2 Vertical Histogram
    • 1.13 Horizontal Histogram
    • 1.14 Histogram of Frequency of Characters
    • 1.15 Temperature Convertor using function call
    • 1.16 Print length of arbitrary long input line
    • 1.17 Print lines that are longer than 80 chars
    • 1.18 Remove trailing blanks and tabs
    • 1.19 reverse a string
    • 1.20 detab, replaces tabs with spaces
    • 1.21 entab, replaces spaces with tabs
    • 1.22 fold long lines
    • 1.23 Remove comments from a C program
    • 1.24 Check rudimentary Syntax Errors in a C Program
  • Types, Operators and Expressions
  • Control Flow
  • Functions and Program Structure
  • Pointers and Arrays
  • Structures
  • Input and Output
  • The UNIX System Interface
  • C Programming Building Blocks
  • C Concepts Visualization

1.17 Print lines that are longer than 80 chars¶

Question¶

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

Solution¶

/**
 *
 * Exercise 1.17 -  Program to print the length of all input lines greater
 * than 80 characters.
 *
 **/

#include <stdio.h>
static const char *_input = "short\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n";
static int _pos = 0;
#define getchar() (_input[_pos] ? (int)(unsigned char)_input[_pos++] : EOF)

#define MAXLINE 1000
#define LIMIT 80

/***
 *
 * We call it _getline, for new getline so that it does not conflict with
 * system function getline
 *
 ***/

int _getline(char line[], int lim);

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

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

    return 0;
}

int _getline(char line[], int lim) {
    int i, c;

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

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

    return i;
}

Explanation¶

A string in C is a character array which ends in \0. getline is a function in our program, which reads one character at a time using getchar and stores it in a character array called s[] and it returns the length of the array.

When an array is sent as argument to the function, like line[MAXLINE] is sent to getline function, the value is passed by reference, which means that any changes the function makes will be available to the main program.

getline returns the length of the line and in our main program, if the length is greater than 80 characters we print it.

Visualize the Concept¶

<Page contents

>Page contents:

  • 1.17 Print lines that are longer than 80 chars
    • Question
    • Solution
      • Explanation
    • Visualize the Concept
<1.16 Print length of arbitrary long input line
1.18 Remove trailing blanks and tabs>
© Copyright 2026 Senthil Kumaran. Last updated on 25 May, 26.

Styled using the Piccolo Theme