CMSC 223 Systems Programming
Fall 2023


Assignment#2
Due in Class on Wednesday, September 27, 2023


This assignment requires you are 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. 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 173

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.

2. LETTER FREQUENCY COUNT: Write a program to read a file of text (using standard input and redirection) and print out the frequencies of occurrence of each letter in the text, in ascending order. This is very similar to the first program, except you will now keep track of letters. Recall from class and your text that you can use char values as numerical values. You will use that to index into an array of letter counts. You can use Linux commands to help you solve the problem. For example, first write a program that outputs all the letter frequencies as shown below:

Sample Input Text: Everyone can be taught to sculpt: Michelangelo would have had to be taught not to. So it is with great programmers.

 A    7
 B    2
 C    3
 D    2
 E    10
 F    0
 G    5
 H    6
 I    4
 J    0
 K    0
 L    4
 M    3
 N    4
 O    9
 P    2
 Q    0
 R    5
 S    4
 T    12
 U    4
 V    2
 W    2
 X    0
 Y    1
 Z    0

Next, using the sort command, rearrange the output from your program to display the table in descending order of counts.
Submit your output for the above text as well as the file: ~dkumar/cs223/Lab3/Alice.txt
Show the command you used to produce the output. Your submission should be a listing of the program, followed by the outputs from data provided. Also add a short reflection.

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 CMSC223 Home page