Question

In: Computer Science

Write a program in c++, with at least four functions, including main, which must do the...

Write a program in c++, with at least four functions, including main, which must do the following:

  1. Ask user whether they want to encode or decode a message – if no, then terminate
  2. Take the input string from the user, store it in dynamic memory (use new)
  3. As appropriate, encode or decode the message using Rot13.
  4. Output the encoded/decoded message
  5. Delete the input string from dynamic memory (use delete)

Input will be a string of no more than 25 characters. Blanks get replaced with blanks.

Do not worry about punctuation; there will be no punctuation in the string.

ALPHABET becomes NYCUNORG

Test your program with the following strings:

TAF VF

paddrpf

Strings Will be used to test code!

Some suggestions (NOT requirements):

  1. C++ string library functions would be useful here
  2. You can process the C++ string one char at a time if you use the length member function

Decryption Key

A|B|C|D|E|F|G|H|I|J|K|L|M
-------------------------
N|O|P|Q|R|S|T|U|V|W|X|Y|Z

(letter above equals below, and vice versa)

As you can see, A becomes N, B becomes O and so on.

Main

-user input processing -repeat if wanted

   -clean up of dynamic storage (delete)

|

_____________________________ |____________________________

   | | |

Input encrypt/decrypt output

-read string -read from dynamic storage -get results

-put into dynamic storage -encrypt/decrypt -output results

   -store in dynamic storage

Solutions

Expert Solution

C++ Program:

/* C++ Program that decrypts the given cipher text by left rotating by 13 */

#include <iostream>
#include <cstring>
#include <stdlib.h>

using namespace std;

//Function prototype
char* decrypt(char*);
char* encrypt(char*);
char* inputString();

//Main function
int main()
{
int opt;
char *str, *opstr;

while(1)
{
//Printing menu
cout << "\n\n ***** MENU ***** \n 1-Encryption \n 2-Decryption \n 3-Exit \n\n Your option: ";
cin >> opt;

if(opt == 1)
{
//Reading string
str = inputString();
opstr = encrypt(str);
cout << "\n Encrypted: " << opstr;
delete[] str;
}
else if(opt == 2)
{
//Reading string
str = inputString();
opstr = decrypt(str);
cout << "\n Decrypted: " << opstr;
delete[] str;
}
else
{
return 0;
}
}

return 0;
}

//Function that reads the string
char* inputString()
{
char *str;

//Allocating memory
str = new char[25];

cin.ignore();

cout << "\n Enter String: ";
cin.getline(str, 100);

return str;
}

//Function that decodes given message using left rotational shift key
char* decrypt(char* cipher)
{
int i,j;

//Shift value 13
int val, shift=13;

//Character array
char *decodedMesg = new char[25];

//Looping over each character
for(i=0; i<strlen(cipher); i++)
{
//Getting Ascii values
val = (int)(cipher[i]);

//For Upper case letters
if(val >= 65 && val <= 90)
{
//Boundary crossing condition
if( (val - shift) < 65 )
val = 90 - abs(( (val - shift) - 65 )) + 1;
else
val = val - shift;

//Decrypting
decodedMesg[i] = (char)(val);
}
//For Lower case letters
else if(val >= 97 && val <= 122)
{
//Lower case
if( (val - shift) < 97 )
val = 122 - abs(( (val - shift) - 97 )) + 1;
else
val = val - shift;

//Decrypting
decodedMesg[i] = (char)(val);
}
//Other than letters
else
decodedMesg[i] = cipher[i];
}

//Adding string termination character
decodedMesg[i] = '\0';

//Returning the plain text
return decodedMesg;
}


//Function that decodes given message using left rotational shift key
char* encrypt(char* plain)
{
int i,j;

//Shift value 13
int val, shift=13;

//Character array
char *decodedMesg = new char[25];

//Looping over each character
for(i=0; i<strlen(plain); i++)
{
//Getting Ascii values
val = (int)(plain[i]);

//For Upper case letters
if(val >= 65 && val <= 90)
{
//Boundary crossing condition
if( (val + shift) > 90 )
val = 65 + abs(( 90 - (val + shift) )) - 1;
else
val = val + shift;

//Decrypting
decodedMesg[i] = (char)(val);
}
//For Lower case letters
else if(val >= 97 && val <= 122)
{
//Lower case
if( (val + shift) > 122 )
val = 97 + abs(( 122 - (val + shift) )) - 1;
else
val = val + shift;

//Decrypting
decodedMesg[i] = (char)(val);
}
//Other than letters
else
decodedMesg[i] = plain[i];
}

//Adding string termination character
decodedMesg[i] = '\0';

//Returning the plain text
return decodedMesg;
}
____________________________________________________________________________________________

Sample Run:


Related Solutions

Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Write a complete C++ program that at least consists of the main() function and at least...
Write a complete C++ program that at least consists of the main() function and at least two recursive functions. The first function has no return value and can be named printPrime(). It prints first n prime numbers with proper prompt. Note that number 1 is not regarded as a prime number. We assume the first prime number is 2. The printout should start from 2. The prototype of the recursive function should be void printPrime(int n); The algorithm of printPrime()...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what...
Write a small C++ program with 4 functions (and main(): getNumbers()- what is the return, what are the parameters? findMax()- what is the return, what are the parameters? findMin()-what is the return, what are the parameters? find()- should return the index of the element or a -1 indicating not found The main function will call those methods and print the results of each. 1 // declare necessary variables 2 // declare array 3 double numbers[SIZE]; 4 // Function prototypes 5...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow...
python code Write a simple calculator: This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers,...
This program must have 9 functions: •main() Controls the flow of the program (calls the other...
This program must have 9 functions: •main() Controls the flow of the program (calls the other modules) •userInput() Asks the user to enter two numbers •add() Accepts two numbers, returns the sum •subtract() Accepts two numbers, returns the difference of the first number minus the second number •multiply() Accepts two numbers, returns the product •divide() Accepts two numbers, returns the quotient of the first number divided by the second number •modulo() Accepts two numbers, returns the modulo of the first...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three integer values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write a complete Java program, including comments in each method and in the main program, to...
Write a complete Java program, including comments in each method and in the main program, to do the following: Outline: The main program will read in a group of three int||eger values which represent a student's SAT scores. The main program will call a method to determine if these three scores are all valid--valid means in the range from 200 to 800, including both end points, and is a multiple of 10 (ends in a 0). If the scores are...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main –...
Write an IPO diagram and Python program that has two functions, main and determine_grade. main – Should accept input of five numeric grades from the user USING A LOOP.   It should then calculate the average numeric grade.    The numeric average should be passed to the determine_grade function. determine_grade – should display the letter grade to the user based on the numeric average:        Greater than 90: A 80-89:                 B 70-79:                 C 60-69:              D Below 60:           F Modularity:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT