CS246 Homework 1:  chars and loops

Due: Wednesday, Feb 6, 2012 11:59pm

Please read the homework guidelines before you proceed.

Part 1: Counting [30pts]

Using only the features of C you have learned in so far write a C program that takes text as input, and outputs the following:

the number of lines in the input
the number of characters in the input
the number of words in the input

Effectively, we're building the Unix program wc.

All characters, including the newline ('\n'), should be counted.  Assume that only space(s), newline characters, and tab characters seperate words. That is: ' ', '\n', and '\t'.

Input is terminated by entering CTRL-D at any time.  Recall that CTRL-D triggers an EOF.

Instead of typing long paragraphs to test your program, you can use text file using I/O redirection. I/O redirection on Unix converts text files to standard input for programs. For example, if the text is stored in a file, textinput, you can input it into the program (a.out) by using I/O redirection as follows:

$ ./a.out < textinput        (note that the $ denotes the command prompt; you would not type the $)

The results will then be printed on the screen. Alternately, you can do:

$ ./a.out < textinput > results

The results will be printed in a file called, results

Use the unix program wc to verify the correctness of your program.

Part 2: Sum of numbers as characters [70pts]

Write a program that reads numbers in one by one, and then add them up. Sounds easy right? Well, the catch is, the numbers are read in as characters.

Your program must do the following:


Extra Credits (10 points):

  1. Leading 0 in a floating point number is optional, that is, .4 and 0.4 are equivalent and both acceptable.
  2. Handle negative numbers correctly.