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...
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
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...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of...
Write the code in C++. Write a program to implement Employee Directory. The main purpose of the class is to get the data, store it into a file, perform some operations and display data. For the purpose mentioned above, you should write a template class Directory for storing the data empID(template), empFirstName(string), empLastName(string), empContactNumber(string) and empAddress(string) of each Employee in a file EmployeeDirectory.txt. 1. Write a function Add to write the above mentioned contact details of the employee into EmployeeDirectory.txt....
*Please write code in C++* Write a program to verify the validity of the user entered...
*Please write code in C++* Write a program to verify the validity of the user entered email address.   if email is valid : output the stating that given email is valid. ex: "The email [email protected] is valid" else : output the statement that the email is invalid and list all the violations ex:  "The email sarahwinchester.com is invalid" * @ symbol * Missing Domain name The program should keep validating emails until user enter 'q' Upload your source code. ex: main.cpp
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT