Saturday 22 January 2011

The C Programming Language (K&R) 01x14—Char Arrays & Functions—Exercises 1-16-18


Still on Section 1.9 of “The C Programming Language” by Brian Kernighan and Dennis Ritchie aka K&R and their discussion of character arrays and how to use them as an argument for a function. This entry continues from here. That entry deals with the sample code in the book on finding the longest line of text entered by a user on the keyboard. This entry deals with their exercises 1-16 to 1-18.
 
Exercise 1-16. 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.

It’s not clear to me what this exercise is about. I suspect it has to do with how console inputting works on their system. Does input stop when it hits the far right of the screen? Not sure. If the problem arises because only the first N characters are printed per line, then see below.

Exercise 1-17. Write a program to print all input lines that are longer than 80 characters.

Because of how the getchar() function is implemented in my compiler, this issue doesn’t show up. I can type lines far longer than 80 characters without it creating any problems, therefore, I can’t write code to fix a problem that doesn’t exit. I could simulate the problem, but won’t.

If you want to print only 80 characters per line, you would use a loop that prints when the modulus of the character counter and 80 is zero except when you when you hit the null character and you print what’s left.

Exercise 1-18. Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

This exercise will be the focus of my sample code, but I have some questions. It’s seems the trailing blanks are from the last word to the end of line, but what about the tabs? All tabs or trailing tabs? It’s not clear to me. Let’s assume, it’s only trailing tabs and spaces that should be removed. There’s one problem with this, you won’t see any difference on the screen. Blank, nothing, a space or tab and are all the same. So, I’ll replace spaces with B and tabs with T. That way you can see.

As for blank lines, they exist when the element at index zero is the newline character or ASCII 10. That’s easy enough to find them.

Pseudocode.

While Input is not EOF.
     Get input until line is filled.

     If first character is newline, ignore.
     If first character is NOT newline, replace trailing spaces with B and trailing tabs with T and output line.

Time to write the code.

*    *    *

I had more difficulty coding this exercise than I expected. Part of the delay was learning certain aspects of the language, or making sure I had something correct, but mostly I followed the previous code instead of my pseudocode. Lesson learnt.
 

Sample Code.

I am using Visual C++ 2010 and created the sample code as a console application.

// Function prototype.
int PrintLine(char line[], int maxline);

// The standard library includes the system function.
#include <cstdlib>

// Standard I/O library.
#include <cstdio>

#define MAXLINE 1000 /* maximum input line length */

int main()
{
     int c;
     int i = 0;
     char LineIN[MAXLINE]; /* current input line */

     // Main loop
     while ((c=getchar()) != EOF)
     {
           // Get line of text from user.
           if (i < MAXLINE-1 && c != EOF && c!='\n') {
                // Add char to the line.
                LineIN[i] = c;
                ++i;
           }
           else {
                // Print the line.

                // Newline not stored in for loop.
                if (c == '\n') {
                     LineIN[i] = c;
                     ++i;
                }

                // End of char array.
                LineIN[i] = '\0';

                // Print line.
                PrintLine(LineIN, MAXLINE);

                // Reset counter.
                i = 0;
           } // end if
     } // end while

     // Keep console window open.
     system("pause");

     // Return some value.
     return 0;

} // end main

int PrintLine(char line[], int maxline)
{
     int i, len;

     // Don't print blank lines.
     if (line[0] == '\n')
     {
           // Show we don't print blank lines.
           printf("Blank line.\n");
           // Return.
           return 0;
     }

     // Find len of the char array.
     // Normally look for '\0' but here it's newline char.
     for (len = 0; line[len] != '\n'; ++len)
           ;

     // Look for trailing tabs or spaces.
     while (len)
     {
           --len;
           if (line[len] == ' ')
                line[len] = 'B';
           else if (line[len] == '\t')
                line[len] = 'T';
           else
                // No more trailing items.
                // End checking.
                len = 0;
     }
    
     // Outline line.
     printf("%s", line);

     // Return something.
     return 0;
} // End PrintLine.


Output.



No comments:

Post a Comment