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

Write a program called \"hw1d\" which can encode and decode ASCII text files, i.

ID: 3878582 • Letter: W

Question

Write a program called "hw1d" which can encode and decode ASCII text files, i.e., any file you can view and make sense of such as source code or an html file. Since the goal is not to prevent bad guys from reading your files, we take a simple approach based on systematic character shifting.

Computers represent alphanumeric characters using what is called an ASCII code which is nothing more than a a unique 8-bit number. For example, newline ' ' is 10 (0x0A), space ' ' is 32 (0x20), digit '0' is 48 (0x30), letter 'A' is 65 (0x41) while letter 'a' is 97 (0x61) and so forth. (the Unix command "man 7 ascii" will show you all the encodings). Since alphanumeric characters are integer numbers, we can manipulate them. For example, 'a'+1 produces 'b' and 'a'+2 produces 'c'. We will use this idea to change all letters the same way. To keep things interesting, we will make sure lower case letters remain lower case and upper case letters remain upper case. We will not change white space, digits, and other characters. In order to this, you need to determine whether a character represents a letter and if so, whether it is lower or upper case. Use the library functions "isalpha", "islower", and "isupper" to get that job done. More about these below. The tricky bit is to ensure the encoding stays within the realm of lower and upper case letters. For example, 'z'+1 should be mapped to 'a', 'y'+4 should be mapped to 'c', and so forth. When you decode, you likewise need to ensure that say 'a'-1 maps to 'z' and 'c'-4 maps to 'y'. This is not hard but you do need to be careful when you implement these cyclic mappings.

How will the program know whether to encode or decode and how will it know which number to add or subtract to each letter? You tell it using command line arguments. The first is either "-encode" or "-decode". The second is an integer in the range 0-9.

Your program must check that the correct number and type of command line arguments is given. If not, an error message should be printed to stderr and program execution stopped. The encoding and decoding should be carried out on a textline-by-textline basis using functions "encode(string&,int)" and "decode(string&,int)". The two arguments are a reference to a textline and the magic shift you will apply to the lower and upper case letters in it. The function "getline(istream&, string&)" can be used to read textlines one at a time.

Explanation / Answer

Given below is the program to perform encoding and decoding as described in the question. Output is shown at end. Please don't forget to rate the answer if it helped. Thank you very much.

#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
using namespace std;

string encode(const string &str, int shift);
string decode(const string &str, int shift);

int main(int argc, char *argv[])
{
  
if(argc != 3 || !(strcmp(argv[1], "-encode") == 0 || strcmp(argv[1], "-decode") == 0))
{
cout << "usage: " << argv[0] << " -encode | -decode <shift_number>" << endl;
return 1;
}
  
int shift = atoi(argv[2]);
  
string line;
getline(cin, line);
  
if(strcmp(argv[1], "-encode") == 0 )
cout << encode(line, shift) << endl;
else
cout << decode(line, shift) << endl;
  
}

string encode(const string &str, int shift)
{
string result = "";
char ch;
for(int i = 0; i < str.length(); i++)
{
ch = str[i];
if(!isalpha(ch)) //not a alphabet
result += ch;
else
{
int val ;
  
if(islower(ch))
{
val = ch - 'a'; //convert the ascii character to int in range 0 - 25
val = (val + shift) % 26; //apply the shift forward
val = val + 'a'; //map back the int value to corresponding lower case ascii value
}
else //upper case
{
val = ch - 'A'; //convert the ascii character to int in range 0 - 25
val = (val + shift) % 26; //apply the shift forward
val = val + 'A'; //map back the int value to corresponding upper case ascii value
}
  
result += (char)val;
}
}
return result;
}
string decode(const string &str, int shift)
{
string result = "";
char ch;
  
for(int i = 0; i < str.length(); i++)
{
ch = str[i];
if(!isalpha(ch)) //not a alphabet
result += ch;
else
{
int val;
if(islower(ch))
{
val = ch - 'a'; //convert the ascii character to int in range 0 - 25
val = (val - shift) ; //apply the shift backward
if(val < 0) //if out of range
val += 26;
val = val + 'a'; //map back the int value to corresponding lower case ascii value
}
else //upper case
{
val = ch - 'A'; //convert the ascii character to int in range 0 - 25
val = (val - shift) ; //apply the shift backward
if(val < 0) //if out of range
val += 26;
val = val + 'A'; //map back the int value to corresponding upper case ascii value
}
result += (char)val;
}
}
return result;

}

output

$ g++ hw1d.cpp -o hw1d
$ ./hw1d -encode 4
You can't read this!
Csy ger'x vieh xlmw!

$ ./hw1d -encode 4 | ./hw1d -decode 4
You can't read this!
You can't read this!

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