Previous: 4.2 Sample Character Processing Functions
Up: 4.2 Sample Character Processing Functions
Next: 4.2.2 Converting Digit Characters to Numbers
Previous Page: 4.2 Sample Character Processing Functions

4.2.1 Converting Letter Characters

Our next task is to copy input characters to output as before except that all lower case letters are converted to upper case.

Task

COPY1: Copy input characters to output after converting lower case letters to upper case.

The algorithm is similar to COPY0, except that, before printing, each character it is converted to upper case, if necessary.

read the first character
     repeat as long as NOT end of file
          convert character to upper case
          print the converted character
          read the next character
We will write a function, uppercase(), to convert a character. The function is given a character and if its argument is a lower case letter, uppercase() will return its upper case version; otherwise, it returns the argument character unchanged. The algorithm is:
if lower case convert to upper case,
     otherwise, leave it unchanged;
The prototype for the function is:
char uppercase(char ch);
The code for the driver and the function is shown in Figure 4.7. The driver is straight forward; each character read is printed in its uppercase version.

The while expression is:

((ch = getchar()) != EOF)
Here we have combined the operations of reading a character and testing for EOF into one expression. The innermost parentheses are evaluated first: getchar() reads a character and assigns the returned value to ch. The value of that expression, namely the value assigned to ch, is then compared with EOF. If it is not EOF, the loop executes, otherwise the loop terminates. The inner parentheses are essential. Without them, the expression is:
(ch = getchar() != EOF)
Since the precedence of an assignment operator is the lowest, getchar() reads a character and the returned value is first compared to EOF:
getchar() != EOF
The value of this comparison expression, 0 or 1, is then assigned to ch: the wrong result is in ch. Of course, it is always best to use parentheses whenever there is the slightest doubt. Note, we have used the call to the function, uppercase(), as the argument for the routine, putchar(). The value returned from uppercase() is a character, which is then passed to putchar().

The function, uppercase(), checks if c is a lower case letter (using the macro IS_LOWER()), in which case it returns the upper case version of c. We have used the macro TO_UPPER() for the expression to convert to upper case, making our program more readable. When the return statement is executed, control returns immediately to the calling function, thus, the code after the return statement is not executed. Therefore, in this case we do not need the else part of the if statement. In uppercase(), control progresses beyond the if statement only if c is not a lower case letter, where uppercase() returns c unchanged. A sample session is shown below:



Previous: 4.2 Sample Character Processing Functions
Up: 4.2 Sample Character Processing Functions
Next: 4.2.2 Converting Digit Characters to Numbers
Previous Page: 4.2 Sample Character Processing Functions

tep@wiliki.eng.hawaii.edu
Wed Aug 17 08:29:11 HST 1994