CMSC 246 Systems Programming
Spring 2018


Assignment#2
Due in Class on Wednesday, February 7, 2018


This assignment requires you to write two C programs. Please read the description of the programs carefully. Then, read the chapters in your C & Linux texts that describe the features beiing used. Then, begin working on this assignment.

  1. SHOUTER

    Write a program in file shout.c to input whatever the user types on the keyboard and prints it out, all in uppercase letters. To understand the behavior of the program, try the command

    [xena@codewarriors cs246]$ cat

    When cat is used as above, without any file name, it simply outputs whatever you type in. It will not print out the characters as you type them, but only when you press the <ENTER> key. It will stop running once you type and <end-of-file> character. In the terminal, the <end-of-file> character is entered by typing <CTRL-D> on an empty line.

    Note that the program is required to change all letters to uppercase. You can detect letters by using the isalpha() function, and you can use the toupper() function to convert alphabetic characters to uppercase. Consult the link, or your text for deatils on these functions.

    Extra Credit: Instead of using the toupper() function, use arithmetic on characters to perform the conversion. See the section Character-Handling Functions on page 138 of King.

    You will use getchar() to read in individual characters and print them out.

    Your program should keep running until it reads <end-of-file>, which is indicated by a -1 (or EOF, as shown in class) return value from getchar().

    Show the output of your program by first typing the following as input from the keyboard:

    She sells
    Sea shells
    By the seashore
    in Seychelles


    Next, create a text file tse.txt with the following contents:

    In the room women come and go
    Talking about Michaelangelo
    -: T. S. Eliot


    Run your program using I/O redirection as below:

    xena@codewarriors cs246]$ ./shout < tse.txt


  2. Counting lines, words, characters (wc)

Reimplement the utility wc by reading in characters using getchar() and reporting how many lines, words, and characters have been read. Whitespace (which separates all words) can be detected by isspace(). Lines are separated by newlines ('\n'). Name your program mywc.c.

For example, if you type the following in (followed by Ctrl+D),

One of the main causes of the fall of the Roman Empire was that–--lacking zero–--they
had no way to indicate successful termination of their C programs.
-- Robert Firth

your program should produce

3 29 175

Note that an empty input (an immediate Ctrl+D) should produce three 0s. You can check the correctness of your program against the wc command. In your report, show the output of your program when you enter the text above from the keyboard. Also, using I/O redirection as in Part 1, above, show the output of your program on the tse.txt file.

Scripting in Linux
Once done, you will need to show the sample runs of programs, as required above. In order to record the runs, you can use the Linux command script.
Go to your Assignment2 directory and enter the command: script session

You will get your command prompt back. Next, run each of the above programs as you normally would. Once done, enter the command: exit (or CTRL-D)

This will end your script session. If you now look a the contents of the session file, you will see that the entire interaction has been recorded in it (as a script!).

What to hand in:

Back to CMSC246 Home page