Question

In: Computer Science

Write a C++ program which performs a rot 13 substitution. Must be in C++. Thank you.

Write a C++ program which performs a rot 13 substitution. Must be in C++. Thank you.

Solutions

Expert Solution

CodeToCopy:

rot13.cpp

#include <iostream> /* for cin, cout */

using namespace std;

/* string variable holds lower case letters */

string lower_case_letters = "abcdefghijklmnopqrstuvwxyz";

/* string variable holds upper case letters */

string upper_case_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

/* function encodes and decodes the input string using

* rot13 substitution cipher */

string rot13(string input) {

   

    /* string variable to hold output */

    string output;

   

    /* holds the position of input character in alphabet */

    int letter_pos;

   

    /* loop to iterate over input string */

    for (int i = 0; i < input.size(); ++i) {

       

        /* finding the position of input character in lower_case_alphabet */

        letter_pos = lower_case_letters.find(input[i]);

        if (letter_pos < 0) {

           

             /* finding the position of input character in upper_case_alphabet */

            letter_pos = upper_case_letters.find(input[i]);

           

            if (letter_pos < 0) {

                /* its not a character in alphabet, so not doing anything */       

                output.append(1, input[i]);

                continue;

            }

           

            /* getting the position of 13th character from current character */

            letter_pos = (letter_pos + 13) % 26;

           

            /* appending it to output */

            output.append(1, upper_case_letters[letter_pos]);           

        }

       

        else {

            /* getting the position of 13th character from current character */

            letter_pos = (letter_pos + 13) % 26;

           

            /* appending it to output */

            output.append(1, lower_case_letters[letter_pos]);

        }

  }

   

    /* returning input */

    return output;

}

/* driver code to test to above function */

int main() {

   

    /* taking input from the user */

    string input;

    cout << "input: ";

    cin >> input;

   

    /* getting rot13 encoded text and printing */

    string output = rot13(input);

   

    cout << "encoded: " << output << endl;

   

    /* getting rot13 decoded text and printing */

    cout << "decoded: " << rot13(output) << endl;

   

   

    return 0;

}

OutputScreenshot:



Related Solutions

Write a C program A simple method for encrypting data is that of ROT 13. The...
Write a C program A simple method for encrypting data is that of ROT 13. The method takes each latin letter of plain text and moves it to the next letter 13 times in latin alphabet (wrapping around if necessary). For those of you who are unaware the latin alphabet is the following 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 This...
write a C program which performs encryption and decryption of a message
write a C program which performs encryption and decryption of a message
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...
Your task is to write a C program which performs encryption and decryption of a message...
Your task is to write a C program which performs encryption and decryption of a message using the substitution cipher algorithm. Write a C program which performs encryption and decryption using the substitution cipher algorithm. Your program must be fully automated (ie: it does not take any interactive user input) and either encrypt or decrypt depending on the files which exist in the program’s directory. If message.txt exists your program should read that file, encrypt the contents, and write the...
I need specific codes for this C program assignment. Thank you! C program question: Write a...
I need specific codes for this C program assignment. Thank you! C program question: Write a small C program connect.c that: 1. Initializes an array id of N elements with the value of the index of the array. 2. Reads from the keyboard or the command line a set of two integer numbers (p and q) until it encounters EOF or CTL - D 3. Given the two numbers, your program should connect them by going through the array and...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement:...
Program this in C thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output a string that is the concatenation of string str1...
You should write a small C++ program that performs the following tasks: First, your program should...
You should write a small C++ program that performs the following tasks: First, your program should read in a set of 5 integers from “standard in” (remember cin). The numbers that you read in will be either zero or positive. If at least one of the five numbers entered is non-zero then your program should calculate the sum of the numbers and report that back to the user using “standard output” (remember cout). Then your program should be ready to...
Please write code in C, thank you. Write a program that reads a list of integers,...
Please write code in C, thank you. Write a program that reads a list of integers, and outputs whether the list contains all even numbers, odd numbers, or neither. The input begins with an integer indicating the number of integers that follow. Assume that the list will always contain less than 20 integers. Ex: If the input is: 5 2 4 6 8 10 the output is: all even Ex: If the input is: 5 1 3 5 7 9...
In C: Write a complete program that performs the following task: Ask the user for the...
In C: Write a complete program that performs the following task: Ask the user for the number of sequences to display. For each sequence, Ask the user for a starting value Print out the value and double it (multiply by 2). Continue printing and doubling (all on the same line, separated by one space each) as long as the current number is less than 1000, or until 8 numbers have been printed on the line. You may assume that the...
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: Ask user whether they want to encode or decode a message – if no, then terminate Take the input string from the user, store it in dynamic memory (use new) As appropriate, encode or decode the message using Rot13. Output the encoded/decoded message Delete the input string from dynamic memory (use delete) Input will be a string of no more than 25 characters....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT