Question

In: Computer Science

Write a C++ program performing the rot13 cipher: Basically the code should perform like this: The...

Write a C++ program performing the rot13 cipher:

Basically the code should perform like this:

The user should be able to input any letter or words, or even sentences where once they have inputted the particular word, each letter goes 13 letters ahead, so an 'A' becomes an 'N', a 'C' becomes 'P', and so on. If the rot13 cipher is applied a second time, the original plantext is restored: 'N' becomes 'A', 'P' becomes 'C'. The 13 letters go in a rotation so if for example we have 'Z', 13 letters ahead of it is 'M'. So perform the rot13 cipher using C++.

Solutions

Expert Solution

#include <iostream>

using namespace std;

string rot13encrypt(string orig, int rshift){

string result = "";

rshift = rshift%26;

// traverse text

for (int i=0;i<orig.length();i++)

{

if(orig[i] ==' ' || isdigit(orig[i]) || !isalpha(orig[i]) )

result += orig[i];

else if (isupper(orig[i]))

result += char(int(orig[i]+rshift-65)%26 +65);

// Encrypt Lowercase letters

else

result += char(int(orig[i]+rshift-97)%26 +97);

}

// Return the resulting string

return result;

}

string rot13decrypt(string orig, int rshift){

string result = "";

rshift = rshift%26;

// traverse text

for (int i=0;i<orig.length();i++)

{

if(orig[i] ==' ' || isdigit(orig[i]) || !isalpha(orig[i]) )

result += orig[i];

else if (isupper(orig[i])) {

int v = orig[i]-rshift-65;

int k = (26 + (v%26)) % 26;

result += char(k +65);

}

// Encrypt Lowercase letters

else {

int v = orig[i]-rshift-97;

int k = (26 + (v%26)) % 26;

result += char(k +97);

}

}

// Return the resulting string

return result;

}

void display(string rot13){

cout<<"After rot13 "<<rot13<<endl;

}

int main()

{

string text;

int shift;

cout<<"Enter the text: ";

getline(cin,text);

cout<<"Original String: "<<text<<endl;

string str = rot13encrypt(text,13);

display("Encryption: "+ str);

string rot13 = rot13decrypt(str,13);

display("Decryption: "+ rot13);

}

=========================================

SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.


Related Solutions

Write a C++ program performing the rot13 cipher, The code should perform like this: The user...
Write a C++ program performing the rot13 cipher, The code should perform like this: The user should be able to input any letter or words, or even sentences where once they have inputted the particular word, each letter goes 13 letters ahead, so an 'A' becomes an 'N', a 'C' becomes 'P', and so on. If rot13 cipher is tested a second time, the original plantext should be restored: 'P' becomes 'C', 'N' becomes 'A'. The 13 letters go in...
Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be...
Write an Java/C program code to perform known-plaintext attack on Permutation Cipher. This program can be used to determine the length of the permutation m and the key permutation.
write a program to perform the following in C Your program should prompt the user to...
write a program to perform the following in C Your program should prompt the user to enter ten words, one at a time, which are to be stored in an array of strings. After all of the words have been entered, the list is to be reordered as necessary to place the words into alphabetical order, regardless of case. Once the list is in alphabetical order, the list should be output to the console in order. The program should execute...
Write a C Program that uses file handling operations of C language. The Program should perform...
Write a C Program that uses file handling operations of C language. The Program should perform following operations: 1. The program should accept student names and students’ assignment marks from the user. 2. Values accepted from the user should get saved in a .csv file (.csv files are “comma separated value” files, that can be opened with spreadsheet applications like MS-Excel and also with a normal text editor like Notepad). You should be able to open and view this file...
Write a program with the aim of performing an audiometry test at MATLAB. The program should...
Write a program with the aim of performing an audiometry test at MATLAB. The program should be as interactive as possible. For example, at first, which ear is to be tested should be chosen so that the sound is only given to that channel of the ear. In addition, whether the test frequency increases automatically or manually should be asked as a parameter. The frequencies of the person being tested should be entered by the user as well as whether...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The...
In C++, write a program to implement the Caesar Cipher for both encryption and decryption. The program should be able to handle different keys by deciding the key at run time. Thank you :)
All Code should be written in C: 1. Write a C program which prompts the user...
All Code should be written in C: 1. Write a C program which prompts the user to enter two integer values. Your program should then print out all numbers between 1 and 1000 that are divisible by both of those numbers. 2. Modify your program from question 1 such that the first 1000 numbers that are divisible by both numbers are printed out, instead of numbers up to 1000. 3. Using dynamic memory, allocate memory for an array of 100...
write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should...
write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players". in python
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption...
write a C++ program that : 1. Perform a rot13 substitution 2. Perform a caesar encryption given a dictionary 3. Perform a caesar decryption given a dictionary 4. Create a random caesar cipher dictionary If user prints: -r : Perform rot13 substitution -g : generate a random caesar cipher dictionary. -e: Encrypt using the caesar cipher -d : Decrypt using the caesar cipher The format for the caesar cipher dictionary is a file with 26 pairs of letters, one per...
This needs to be a python3 code Write a program that prompts the user like this:...
This needs to be a python3 code Write a program that prompts the user like this: “Currency to convert to U.S. dollars: e = Euros, c= Chinese Yuan, r = Indian Rupees, b = Bitcoin: ”. Then depending on which letter the user enters, the program displays “Amount of Euros/Yuan/Rupees/Bitcoin to convert: ”. (Note: the second prompt should only name the one currency the user asked to convert, not all four currencies.) After the user enters the amount, the program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT