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 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 :)
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
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...
Write a Java program that will encode plain text into cipher text and decode cipher text...
Write a Java program that will encode plain text into cipher text and decode cipher text into plain text. Create following methods and add them to your class: • a method named encode(String p, int key) that takes a plain text p and encodes it to a cipher text by adding the key value to each alphabet character of plain text p using the ASCII chart. The method should return the encoded String. For example, if p = "attack at...
Code in C# please. Write a program that will use the greedy algorithm. This program will...
Code in C# please. Write a program that will use the greedy algorithm. This program will ask a user to enter the cost of an item. This program will ask the user to enter the amount the user is paying. This program will return the change after subtracting the item cost by the amount paid. Using the greedy algorithm, the code should check for the type of bill. Example: Cost of item is $15.50 User pays a $20 bill $20...
Who knows to do this topic with python code? Write a program to perform the following...
Who knows to do this topic with python code? Write a program to perform the following two tasks: 1. The program will accept a string as input in which all of the words are run together, but the first character of each word is uppercase. Convert the string to a string in which the words are separated by spaces and only the first word starts with an uppercase letter. For example, the string "StopAndSmellTheRose" would be converted to "Stop and...
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT