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

Overview For this assignment, write a program to decode an encoded text message

ID: 3620906 • Letter: O

Question

Overview
For this assignment, write a program to decode an encoded text message and count the different types of characters.

Processing
The encoded text message will be processed one character at a time. Each character will be decoded by calling an appropriate function and then displayed. Counters for specific types of characters will also be maintained.

The basic layout for your program is as follows:

ifstream inFile;

inFile.open( "encoded_beer.txt" )
if( inFile.fail() )
{
cout << "Input file failed to open";
exit(-1);
}

inFile >> ch;

while ( inFile )
{
//Process the 1 character by decoding it. A cascading if statement should
//be used to test the "type" of character (alphabetic, digit, punctuation,
//etc...) and call the appropriate decoding function and increment the
//appropriate counter

//Display the decoded character

inFile >> ch;
}

inFile.close();

This code will open an input file called "encoded_beer.txt" and then read it one character at a time. It has already been placed in a CPP file that you can download and use to start your program:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/assign6.cpp

As mentioned earlier, besides the encoding, the program will also count specific types of characters AFTER they have been encoded. Keep track of:

the total number of characters in the file
the number of alphabetic characters
the number of uppercase alphabetic characters
the number of lowercase alphabetic characters
the number of digits
the number of punctuation marks
the number of "special characters" (ie. those that aren't alphabetic, a digit, or punctuation)
the number of newline characters
the number of spaces
These values should be displayed after the encoded text.

Input
For this assignment, the encoded text message is contained in a file that can be downloaded and saved on your computer. It is called encoded_beer.txt and can be found here:

http://faculty.cs.niu.edu/~byrnes/csci240/pgms/encoded_beer.txt

It should be saved in the same location as your CPP file.

The Encoding Process
The program that encoded the original text message used the following rules. Your program will simply undo what it did. The original message that was encoded that will be referred to as "msg" below:

Any character in msg that was an uppercase character was converted to lowercase and then had 1 subtracted from it. So 'B' became 'a', 'C' became 'b', ... The first uppercase letter 'A' was wrapped around to the end of the alphabet so that it became 'z'.

Any character in msg that was a lowercase character was converted to uppercase and then had 1 added to it. So 'a' became 'B', 'b' became 'C', ... The last lowercase letter 'z' was wrapped around to the beginning of the alphabet so that it became 'A'.

Any character in msg that was a digit ('0' ... '9') was converted as follows:

0 --> #
1 --> /
2 --> +
3 --> *
4 --> -
5 --> ;
6 --> ^
7 --> @
8 --> $
9 --> !

Certain punctuation was encoded as shown below. Any punctuation that is not shown here was not altered at all.

, --> 9
" --> 8
$ --> 7
: --> 6
? --> 5
' (single quote) --> 4
( --> 3
) --> 2
. --> 1
- --> 0

Two white space characters were encoded as non-standard characters:

a blank/space was encoded as an ASCII 22
a newline character was encoded as an ASCII 20

Note again: any character that does not fit the criteria listed above in 1 - 5 was not altered in the encoding.

The Functions
The decoding will be done by calling the appropriate function. The following set of functions are standard library functions that you can simply call in your program (ie. you do no have to write the code them): isalpha, isupper(), islower(), ispunct(), isdigit().

Each of the functions listed above take a character as an argument and returns a true or false value:

isalpha() will return true if the passed in character is an alphabetic character; and false if the passed in character is not an alphabetic character

isupper() will return true if the passed in character is an uppercase character; and false if the passed in character is not an uppercase character

islower() will return true if the passed in character is a lowercase character; and false if the passed in character is not a lowercase character

ispunct() will return true if the passed in character is a punctuation character; and false if the passed in character is not a punctuation character

isdigit() will return true if the passed in character is a digit; and false if the passed in character is not a digit

The return value allows for the functions to be called as the condition in an if statement:

if( isdigit(ch) )
{
call appropriate decoding function
}

The functions listed below will be used to decode a character (these are the functions you will have to write and call in your program).

char convUpper( char ch )
This function will be called for any character that is an uppercase character. It takes a single character as its argument: the character to be decoded. It returns the decoded character. The function should reverse the process that was detailed in #2 of the Encoding Process above by converting the passed in character to lowercase and subtracting 1, so that, for example, 'C' becomes 'b'. However, there is a special case: 'A' should become 'z'.

char convLower( char ch )
This function will be called for any character that is a lowercase character. It takes a single character as its argument: the character to be decoded. It returns the decoded character. The function should reverse the process that was detailed in #1 of the Encoding Process above by converting the passed in character to uppercase and adding 1, so that, for example, 'd' becomes 'E'. However, there is a special case: 'z' should become 'A'.

char convPunct( char ch )
This function will be called for any character that is a punctuation symbol (in C++, that is all type-able characters that are not letters, digits, or whitespace). It takes a single character as its argument: the character to possibly be decoded. It returns the decoded character. The function should use a switch statement to check for each of the punctuation symbols that were listed under #3 of the Encoding Process above and convert the passed in character to its decoded version, so that, for example, ';' becomes '5'. Don't forget that if the punctuation symbol is not listed above, it should be returned unchanged.

char convDigit( char ch )
This function will be called for any character that is a digit. It takes a single character as its argument: the character to be decoded. It returns the decoded character. The function should use a switch statement to check for each of the digits that were listed under #4 of the Encoding Process above and convert the passed in character to its decoded version, so that, for example, '6' becomes '&'. Since all of the digits were encoded, there will be no case of an argument value not being altered.

char convSpecial( char ch )
This function will be called for any character that is not an uppercase, lowercase, punctuation, or digit character. It takes a single character as its argument: the character to possibly be decoded. It returns the decoded character. The function will only change the passed in character if it has ASCII value 20 or 22. If the passed in character has ASCII value 20, then it will return a newline character. If the passed in character has an ASCII value of 22, then it will return a space. Anything else will be returned unchanged.

Requirements
At the top of your C++ source code, include a documentation box that resembles the ones from the previous programs.

Complete program documentation is required from this assignment. See the documentation standards on the course webpage. Be sure to read the portion about function documentation.

Hand in a copy of your source code using the electronic submission program and a printed copy in the drop box in the CSL computing lab.

Notes
This program can be developed incrementally if you use your own data file. Start by writing the program so that it handles uppercase character. Create a text file with the line ABC. Run your program using this file as input. It should produce zab on the screen as its result. Then add code to handle lowercase characters and add lowercase characters to your text file. Run the program again and verify the output. Add in digits, punctuation, spaces, newlines, etc...

Above, it says that isdigit() and the other "is" functions return a true or false value. That's part of the truth. If you look up the functions, you will see that they actually return an integer. As mentioned in lecture, any non-zero value is considered true; whereas 0 is considered false.

Output
Using the encoded_beer.txt file mentioned above produces the following output:

When Advertising Age magazine released its picks for the best 100 ad
campaigns of the 20th century, it was no surprise that the world of beer
advertising was well represented. After all, few can forget Bubba Smith and
Dick Butkus arguing that eternal debate, "Tastes Great--Less Filling."
Likewise, many a beer drinker can still whistle that infectious jingle,
"Hey Mabel--Black Label," though the popular television commercials have not
aired for 30 years. So, what made these and other classic beer commercials
great?

Surely, from the beer maker's standpoint, a commercial's success can
ultimately be judged by only one criterion: its impact on beer sales. But,
we, the oft-jaded viewers, take a more visceral approach. More and more, we
tend to grade commercials on their ability to, if only in passing, penetrate
our popular culture. At their best, we induct them into our collective
psyche, muse over them with friends and coworkers, and even add their lingo
to our vocabulary (can you say "Whassup?").

Beer makers have been searching for the perfect beer commercial nearly since
television exploded onto the American scene in the late 1940s. In those
pioneer days, nobody--not the advertisers, not the ad agencies, not the TV
stations--knew exactly what made for a good commercial. Indeed, the earliest
beer commercials consisted of everything from live demonstrations of how to
cook a Welsh rarebit using beer to the noisy rumble of a studio audience
muddling through a rendition of the brewer's theme song.

With National Prohibition still fresh in memory, brewers were initially wary
of peddling their beers on the air. Early critics of television saw the new
medium as little more than an intrusion into peoples' living rooms, and many
were concerned that beer ads might offend the viewers' sensibilities.
Commercials that actually showed a person consuming beer, for example, were
often deemed in bad taste. Beer ads were typically aired only in the late
evenings, and Sundays were entirely off limits. Surveys were periodically
conducted among viewers to determine whether any "moral backlash" might be
caused by selling beer on television.

But early apprehension was soon overtaken by the realization that television
offered beer makers something tremendously valuable and unique: the ability
to target the beer drinker right at the barstool. The American tavern, after
all, was the first home of television. In Chicago, for example, taverns
accounted for half of all sales of television sets in 1947. Had any tavern
keeper initially doubted the revolutionary importance of TV to his trade, he
was surely converted after the 1947 World Series. Telecasts of the seven
games between the Dodgers and the Yankees made for standing-room-only crowds
in taverns throughout New York City.

Indeed, in the early days, as TV stations were starved for quality programs,
television was necessarily dominated by sporting events. This, of course,
added significantly to TV's allure among beer advertisers. The notion that
"sports sells beer" is perhaps the most sacred axiom of beer marketing, just
as true 50 years ago as today.

Total Number of Characters 3132

Alphabetic Characters 2513
Lowercase 2452
Uppercase 61

Digits 21

Special Characters 504
Spaces 454
Newlines 50

Punctuation 94

Explanation / Answer

Dear, Here is the code //Header file section #include #include using namespace std; int main() { int totcharacters=0; int lowerCount=0; int upperCount=0; int Digits=0; int lines=0; int spaces=0; int punct=0; int special=0; int alphbets=0; ifstream infile; infile.open("encoded_beer.txt"); if(!infile) { cout