Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For this assignment, you will write a program that reads lines of characters fro

ID: 3640166 • Letter: F

Question

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 ' ' 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.

always increment one counter for each and every character (except the newlines and the terminating '.')
increment a "letter counter" if it's a letter (see isAlpha() below)
If and only if the char is a letter, test to see if the letter is upper or lower case and increment the appropriate counter (see isUpper() and isLower() below).
increment a "digit counter" if it's a digit (see isDigit() below)
increment a "punctuation counter" if the char is a punctuation character. Use the built-in function ispunct() which returns non-zero if its argument is a printing character but neither alphanumeric nor a space. Otherwise, zero is returned. Note that if the character is punctuation, you don't know what non-zero number is returned, so make your test based on 0 or not-zero.
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.

The 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.

Processing Requirements
At the top of the C++ source code, include a documentation box like normal. Complete program documentation is required for this assignment, which include documentation boxes for *EACH* function.

Hand in a copy of the source code (CPP file) using Blackboard.

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

.

Explanation / Answer

The documentation has also been included along with the whole program....:)..

/**
***************************************************
* Created by .                                    *
* User:                                           *
* Date: 3/7/12                                    *
* Time: 11:22 AM                                  *
* DESC:This program takes multiple line input     *
* from the user until a "." isn,t encountered.    *
* It then converts it into upper case, calculates *
* the number of uppercase lowercase letters and   *
* digits and also punctuation marks.              *
***************************************************
* 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.                                      *
***************************************************
*/
#include<iostream>
#include<string>
using namespace std;
int main()
{
     cout<<"Start with brief instructions for the user:";
     cout<<" Type a series of lines to get character ";
     cout<<"counts for each line. Type a line consisting";
     cout<<" of a single period '.' to stop. ";
     int charCounter=0;
     int alphaCounter=0;
     int digitCounter=0;
     int capCounter=0;
     int lowCounter=0;
     int punctCounter=0;
     int i;
     locale loc;
     char a[500],ch;
     for(i=0;i<500;i++)
         {
             a[i]='';
         }
     cin.get(ch);
     while(ch!='.')
     {
         while(ch!=' ')
         {
             a[charCounter]=toupper(ch,loc);
             charCounter++;
             if(isalpha(ch)) alphaCounter++;
             digitCounter += isdigit(ch);
             capCounter += isupper(ch);
             if(islower(ch)) lowCounter++;
             if(ispunct(ch)!=0)punctCounter++;
             cin.get(ch);
         }
         cout<<a;
         cout<<" Char count: "<<charCounter;
         cout<<" Alpha count: "<<alphaCounter;
         cout<<" Cap count: "<<capCounter;
         cout<<" Lower case count: "<<lowCounter;
         cout<<" Punctuation count: "<<punctCounter;
         cout<<" Digit count: "<<digitCounter<<" ";
         for(i=0;i<charCounter;i++)
         {
             a[i]='';
         }
         charCounter=0;
         alphaCounter=0;
         digitCounter=0;
         capCounter=0;
         lowCounter=0;
         punctCounter=0;
         cin.get(ch);
     }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote