Practice Assignment

(proper.c)

Information

  1. For this practice assignment you will write a function that takes a text file that is in all UPPERCASE and converts it to Proper Case. Proper case means that the first word of each sentence is capitalized, and everything else is lowercase. Example:

    This is the input file: input1.txt:

    ASSUME THAT A STUDENT IS TYPING HIS FINAL REPORT FOR ENGLISH CLASS INTO A TEXT FILE. AFTER COMPLETING THE REPORT, HE REALIZED THAT HE MISTAKENLY HAD THE CAPS LOCK KEY ON THROUGHOUT THE TYPING PROCESS. HE SAID OH MY GOSH! WHAT HAVE I DONE? HOW AM I GOING TO FIX THIS? BUT THEN HE KNEW C. SO HE SAID "I WILL WRITE ME A C PROGRAM".

    The file after running the program on it: output1.txt

    Assume that a student is typing his final report for english class into a text file. After completing the report, he realized that he mistakenly had the caps lock key on throughout the typing process. He said oh my gosh! What have i done? How am i going to fix this? But then he knew c. So he said "i will write me a c program".

    This is the prototype for the function:
    void proper(const char *filename);
    
    and this is the command line to compile it:
    gcc -O -Werror -Wall -Wextra -ansi -pedantic main.c proper.c -o proper
    
    Additional tests:

    Here's a driver (main.c) for you: HTML   Text


Notes:

  1. A sentence ends with either a period, exclamation, or question mark. You don't have to support any other characters. ONLY the first character of a sentence is capitalized, NOTHING ELSE. (Yes, it's not proper English, but that is outside the scope of this simple program.)
  2. Except for the first sentence in the file, all sentences will have at least one white space character in front. This means that the decimal point in the floating point number in the text below is NOT marking the end of the sentence:

  3. You can assume that the longest line in a file is 200 characters.
  4. Open the file for input in text/translate mode (i.e. "r") and write the output to stdout. It is probably easiest to read one character at a time (fgetc) and then write one character at a time.
  5. You should use the tolower function to convert the uppercase characters to lower case.
  6. You also might want to look at the isspace function that returns true if the character passed to it is a whitespace character and the ungetc function that lets you put a character back in the input stream.
  7. If you can't open the file, you are to print out a message like this:
    Can't open filename.
    
    with filename replaced by the actual file you were trying to open.
  8. If you want to make your own text files (for testing) that are all uppercase, you can use the tr command to convert text files:
    tr '[:lower:]' '[:upper:] < somefile.txt
    
    Windows does not provide this tool, but it is available as part of the Cygwin tools. (It should already be installed and avialable on Linux and Mac).