Long Lines

If I were well-versed in a handful of programming languages, I’m sure I’d find one better than C for examining line lengths in a text file. Perhaps Perl could do the trick? or even the shell language? Regardless, I know C.

I forget what motivated me to write the long lines utility, which I call width on my system. I needed to examine text files and determine the maximum line length. Rather than incorporate a maximum line length function into a larger code, I wrote a filter. The program processes standard input, counting characters in each line. The longest line length is output when the program is done.

Here’s the code:

#include <stdio.h>

int main()
{
    int ch,count,max;

    /* initialize */
    count = max = 0;

    while(1)
    {
        ch = getchar();
        if( ch == EOF )
            break;
        if( ch == '\n' )
        {
            if( count > max )
                max = count;
            count = 0;
        }
        else
        {
            count++;
        }
    }
    printf("Longest line has %d characters.\n",max);

    return(0);
}

Like any C language filter, the program relies upon an endless loop where getchar() and putchar() statements gobble input and generate output — though for this code, I don’t need the output part. Only Line 12 accepts input.

The if test at Line 13 checks for the EOF (end-of-file marker), which breaks the loop.

The loop checks for the newline character at Line 15. When found, the line has been read and the value of variable count is compared with variable max. If count is greater, it becomes the current maximum line length value. Variable count is then reset to zero.

Otherwise, the else condition increments variable count, tallying another character in the line.

After the file is read, a printf() statement outputs the results, the value of variable max.

Here’s a sample run with standard input:

$ ./a.out
123
1234567890
23
Longest line has 10 characters.

And another when processing the source code file as input:

$ ./a.out < 2019_04_27-Lesson-a.c
Longest line has 49 characters.

Another useful tidbit of information would be the number of the longest line. To implement this modification, I’ve added a variable line initialized to zero. It increments with each newline. When the new value for max is set, a second value maxline is also set to reflect the longest line.

Here is the updated code:

#include <stdio.h>

int main()
{
    int ch,count,max,line,maxline;

    /* initialize */
    count = max = maxline = 0;
    line = 1;

    while(1)
    {
        ch = getchar();
        if( ch == EOF )
            break;
        if( ch == '\n' )
        {
            if( count > max )
            {
                max = count;
                maxline = line;
            }
            count = 0;
            line++;
        }
        else
        {
            count++;
        }
    }
    printf("Line %d has %d characters.\n",maxline,max);

    return(0);
}

And sample output using the above source code:

$ ./a.out < 2019_04_27-Lesson-b.c
Line 31 has 52 characters.

In my actual width program, other options are available: I have the code sift through command line switches to output maximum and minimum lines, as well as output the line itself. All this overhead makes me wonder again why I originally needed the utility or whether I was just bored and kept coding the utility. Probably it was the latter.

Leave a Reply