CSCI 240 - Assignment 6 - Spring 2007
Points: 100
Due: Friday, March 2, 3 pm
Overview
For this assignment, you will write a program that reads lines of characters from the keyboard. For each line, it will count several categories of characters (letters, digits, etc.) in the line and then present summary totals after it has read the input characters. It will read multiple lines reporting on each line until the user enters a line beginning with '.'
This program is not long or complex. But there are several new ideas and features to use. There are a lot of details, hints, and requirements in this assignment. So read it carefully - there is some new information in the assignment itself which is not in the lecture notes. Read it several times before you begin work on the program; make sure you clearly understand what is explained and required here.
When you think you are done, read the assignment again carefully and compare it to what is required to be sure you have followed instructions correctly. (Hint: always do this, for all assignments.)
Input
As described above, the program will process lines of text, one line at a time.
The program will use a new input library function you have not used before:
cin.get(ch) - this function (technically, a "method") reads a single character from standard input and stores it into the char variable passed as an argument. Up to now, we have said that a function cannot directly alter the value of any of its arguments. That was actually a lie. :-) We will soon learn how it can be done. But for now, we will just use this feature.
cin >> ch; does not work well in this program, because it skips over newline chars and spaces. So when you display each character using cout after a plain cin like this, the characters are all jammed together on one long line. Do not use it.
Processing
So the program will read characters - one at a time - from the keyboard Since the program will quit when the first char in a line is a '.' - but will output all counts for the chars in a single line, you can use the following logic (ch is a char variable):
cin.get(ch);
while (ch is not a '.')
{
while (ch != a newline char) // 10 or '\n' is the ASCII code for newline
{
// do everything necessary for this character, including displaying the
// upper case version and incrementing the appropriate counters, then
// get the next character
}
// display all counts and reset counters
}
Note that this is a loop nested inside a loop. The outer loop processes each line, the inner loop processes each char in a line. There is a behavior of cin.get() which you should understand: it will not "wake up" until you type a newline <enter>. So as you type, you will see the characters appear on the screen, but the actual processing of the characters will not begin until you terminate the line by pressing <enter>.
Your program should "echo" - that is, repeat - the inputted line, character-by-character, on the screen. As the program reads each character, it will display it on the screen via cout. However, you must capitalize any lower-case letter when displayed (see toUpper() below). See the Sample Output below. Understand that you will type a line, and see the characters you type, one at a time as you type. After you press <enter> you should see the upper-case version displayed all at once on the next line.
Hint: write the program first without any counters: just make sure you can input and "echo" lines as described above, and also be able to terminate the program with a '.' Then add the counters and the toUpper() and other functions, one at a time as described next.
For each character read, (except the newlines and the '.' that quits the program) your program will increment one or more of several counters (using function calls to determine whether to increment each one or not).
Do this for the characters on each line and reset all the counters to 0 before you read chars in the next line.
Finally, after all the counters have been incremented for all the chars on the current line, display the counters and then reset them all to 0 for the next line.
Functions
Write and use the following 5 functions in your program. Each of the first four of these functions simply tests a character to see if it is or is not of a certain type. They are designed to return a 0 if it is not that type and a 1 if it is of that type, so you can just add the function's return result to the appropriate counter with no decision test:
alphaCounter += isAlpha(ch);
This is simpler than:
if (isAlpha(ch)) // or (isAlpha(ch) == 1) alphaCounter++;
int isAlpha(char ch) - this function returns 1 if ch is any alphabetic letter 'a' thru 'z' or 'A' thru 'Z'. Otherwise it returns 0.
int isDigit(char ch) - this function returns 1 if ch is any digit '0' thru '9'. Otherwise it returns 0.
int isUpper(char ch) - this function returns 1 if ch is any alphabetic letter 'A' thru 'Z'. Otherwise it returns 0.
int isLower(char ch) - this function returns 1 if ch is any alphabetic letter 'a' thru 'z'. Otherwise it returns 0.
char toUpper(char ch) - if the char ch is a lowercase character, this function returns the uppercase version of the letter by subtracting 32 from it. Otherwise it returns the character unchanged. Use the isLower() function to help with this.
Sample Output
Start with brief instructions for the user:
Type a series of lines to get character counts for each line. Type a line consisting of a single period '.' to stop.
Joe Hill JOE HILL Char count: 8 Alpha count: 7 Cap count: 2 Lower case count: 5 Punctuation count: 0 Digit count: 0
One, two, three? ONE, TWO, THREE? Char count: 16 Alpha count: 11 Cap count: 1 Lower case count: 10 Punctuation count: 3 Digit count: 0