|||

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.16 Print length of arbitrary long input line¶

Question¶

Revise the main routine of the longest-line program so it will correctly print the length of arbitrary long input lines, and as much as possible of the text.

Solution¶

/**
 * Exercise 1.16
 *
 * Write a program to print the length of an arbitrarily long input line
 * and as much text as possible
 *
 **/

#include <stdio.h>
static const char *_input = "short\nthis is a much longer line to test the output\n";
static int _pos = 0;
#define getchar() (_input[_pos] ? (int)(unsigned char)_input[_pos++] : EOF)

/* The size of the output that we would like */
#define MAX 1000

/* define our functions*/
int get_line(char arr[], int lim);

void copy(char to[], const char from[]);

int main() {

    int len, max;
    char line[MAX];
    char longest[MAX];

    max = 0;
    while ((len = get_line(line, MAX)) > 0) {

        if (len > max) {

            max = len;
            copy(longest, line);
        }
    }

    if (max > 0) {

        printf("length = %i, string=%s", max, longest);
    }

    return 0;
}

/* get a line in a character array */
int get_line(char arr[], int lim) {

    int c, i;

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

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

        arr[i] = c;
        ++i;

    } else {

        /* Continue to count the length even if it is longer than the max */
        while ((c = getchar() != EOF) && c != '\n') {

            ++i;
        }

        if (c == '\n') {

            arr[i] = c;
            ++i;
        }
    }

    arr[i] = '\0';
    return i;
}

/* copy one character array to another */
void copy(char to[], const char from[]) {

    int i;

    i = 0;

    while ((to[i] = from[i]) != '\0') {

        ++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.

We keep track of each line and it’s length using a variable, max. If the length of the line is greater than max, then we copy that line into the maxline using a copy routine.

At the end of the program, whichever was the longest line that was copied in maxline array, we just print that.

Visualize the Concept¶

<Page contents

>Page contents:

  • 1.16 Print length of arbitrary long input line
    • Question
    • Solution
      • Explanation
    • Visualize the Concept
<1.15 Temperature Convertor using function call
1.17 Print lines that are longer than 80 chars>
© Copyright 2026 Senthil Kumaran. Last updated on 25 May, 26.

Styled using the Piccolo Theme