Winter 2005 CS 31 Section 1 (Shinnerl)

Programming Assignment 2: Character Classification

DUE: 10:00PM Fri. 1/21
Final Specification, 11:20AM, 1/14. Please send questions on this specification to shinnerl@ucla.edu.

Write a C++ program to accept and categorize a single character of input from the user at run-time. Characters categories are the following.

  1. upper-case vowels
  2. upper-case consonants
  3. lower-case vowels
  4. lower-case consonants
  5. digits
  6. special characters
In addition to specifying the character's category from the above list, your program displays the ASCII value of the character. The program need process only those printable characters with ASCII values in the range 33--126. Your program is not required to accept space characters (blank, tab, or newline) or unprintable characters (CTRL-C, etc.); it can simply ignore them. Hence, you may rely on the >> operator for reading in the user's character, because this operator ignores any leading space characters and reads the first non-space character it finds. (It also leaves any other characters after the one read in the input stream, including the trailing '\n' character needed to activate the read operation. This fact will be important in later assignments.)

Sample I/O

Each line in the following table corresponds to a different execution of the program. When the program is run, it prompts the user to enter a single non-space character from the keyboard. This prompt is not shown in the following examples.
Input Output
+ The ASCII value of '+' is 43. '+' is a special character.
a The ASCII value of 'a' is 97. 'a' is a lower-case vowel.
K The ASCII value of 'K' is 75. 'K' is an upper-case consonant.
4 The ASCII value of '4' is 52. '4' is a digit.

Hints.

Suppose your program uses character variable myChar.
  1. Explicit conversion from character to integer type can be done in three ways:
    int(myChar) // C++ style cast
    (int) myChar // C style cast
    static_cast<int>(myChar) // C++ compile-time cast
    However, usually implicit casting is sufficient. Whenever a character appears in an arithmetic expression, the compiler automatically replaces it by its ASCII value: the value of ('A'+1) is 66, same as int('B').
  2. You can reduce the number of branches in your program to about 6. You do not need a separate branch for each character! One branch per category should suffice.
  3. You can write the code for this assignment without knowing or using any specific ASCII values yourself. Let the compiler do the work.
  4. Characters may be compared directly, e.g., the boolean expression (myChar >= 'A' && myChar <= 'Z') is true if and only if the value of myChar is an upper-case letter.
  5. Several functions in the <cctype> library can be used to simplify your coding, e.g., bool isupper(char) (Savitch, pp. 863--4). You may learn and use these functions if you wish. However, at this stage, it is much better to see if you can complete the assignment without use of any libraries other than <iostream>.

Submission

For this assignment, your submission need contain only two files: report.xxx and p2.cpp. (Choose the suffix for the report file to match its type --- .txt, .doc, .html, etc..) Your report should contain your cover page, documentation, and test results. Your source code should be neatly formatted, with control blocks consistently indented and commented.

Homework 2 Home Page