1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
jekas [21]
3 years ago
13

Today we will be making a Caesar Cipher to encrypt and decrypt messages.A Caesar Cipher is a simple cipher that translates one l

etter to another via shifting the letter a few spaces in the alphabet. For example:A Caesar Cipher with a shift of 3 would turn A into D, B into E, C into F, and so on.Your task is to use string methods and two user defined functions (an encrypt function and a decrypt function) to produce an encrypted message from a "plaintext" message and a "plaintext" message from an encrypted message.The program should use a menu such as:1. Encrypt2. Decrypt3. ExitThe menu should be displayed in a loop until the user chooses to exit. When the user chooses the Encrypt option, your program should prompt the user to input a "plaintext" string. Then the program should display what the encrypted text would be.When the user chooses the Decrypt option, your program should prompt the user for an encrypted message and then display the "plaintext" version of that message to the user.For this program each message should be a single word and your cipher should use a shift of 3.Any message entered by the user should be capitalized using a user defined function that will take a string in by reference and use the toupper() function from to capitalize each letter of the string.
Computers and Technology
1 answer:
Len [333]3 years ago
8 0

Answer:

#include <iostream>

#include <stdio.h>

#include <ctype.h>

using namespace std;

string

encrypt (string text, int s)

{  string resultingString = "";

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

   {      if (!isspace (text[i]))

{

  if (isupper (text[i]))

    resultingString += char (int (text[i] + s - 65) % 26 + 65);

  else

  resultingString += char (int (text[i] + s - 97) % 26 + 97);

}

     else

{

  resultingString += " ";

}

  }

 return resultingString;

}

string

decrypt (string text, int s)

{

 string resultingString = "";

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

   {

     if (!isspace (text[i]))

{

  if (isupper (text[i]))

    resultingString += char (int (text[i] + s - 65) % 26 + 65);

  else

  resultingString += char (int (text[i] + s - 97) % 26 + 97);

}

     else

{

  resultingString += " ";

}

   }

 return resultingString;

}

string upper(string str){

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

       str[i]=toupper(str[i]);

   }

   return str;

}

int

main ()

{

 string text = "This is test text string";

 

 int s = 3;

 string cipherText = "";

 string decipherText = "";

int menu=-1;

while (menu!=3){

   cout<<"1. Encrypt"<<endl;

   cout<<"2. Decrypt"<<endl;

   cout<<"3. Exit"<<endl;

   cin >>menu;

   cin.ignore();

   if(menu==1){

       cout<<"Enter Plain string "<<endl;

       getline(cin,text);

       text=upper(text);

         cipherText = encrypt (text, s);

           cout << "cipher text: " << cipherText << endl;

   }

   else if(menu==2){

       cout<<"Enter Encrypted string "<<endl;

       getline(cin,cipherText);

               cipherText=upper(cipherText);

         decipherText = decrypt (cipherText, 26 - s);

 cout << "decipher text: " << decipherText << endl;

   }

   else {

       cout<<"Not valid"<<endl;

   }

}

 return 0;

}

Explanation:

Display menu with options 1 encrypt, 2 decrypt, 3 exit. Write a function to translate string to upper case. Iterate through string and for each index use toupper function to translate alphabet to upper case and store it back to current index of string.On exiting from loop return string. Pass upper case string to encrypt function.

In encrypt function pass string by reference and shift value. Create an empty string resultingString. Iterate through string for each character check if its space, if its a space add it to resulting string otherwise using ascii values convert char to its ascii add shift value and subtract 65 (staring ascii value for capital alphabet) take modules with 26 for new character and than add 65 to get alphabet.Now add this alphabet to resulting string. Upon loop completion return resultingString.

For decryption use same function as encryption. We are using cyclic property to decrypt using same function therefor subtract shift from 26 and pass resulting shift value to decrypt function.

You might be interested in
What is the answer <br> Answer 1<br> Answer 2<br> Answer 3<br> Answer 4
faltersainse [42]

Answer:

Answer 2

Explanation:

8 0
2 years ago
Read 2 more answers
Implement the above in c++, you will write a test program named create_and_test_hash.cc . Your programs should run from the term
likoan [24]
This question is way to long I don’t even understand what your asking
4 0
3 years ago
You can find synonyms and disciplinary jargon in the ______, _______, and ______ in your search results. You can then use these
Slav-nsk [51]

Answer:

1. Title

2. Abstract

3. Subject Headings

Explanation:

To carry out a project or research survey, a researcher needs an appropriate title that satisfies the technical keywords relating to the content and expected outcome of the research.

Hence, in this case, to find an appropriate title for a research survey "You can find synonyms and disciplinary jargon in the TITLE, ABSTRACT, and SUBJECT HEADINGS in your search results. you can then use these as keywords in additional searches."

7 0
3 years ago
A showChar Method
Lady bird [3.3K]

Answer: so I ask my teacher and this is what he said

Explanation:

4 0
3 years ago
When a cells number format is “time” it will show a value in what format
Leokris [45]

The format of time shows a value of time day (what time it is).

When you choose the option of time you can have it displayed in a variety of formats, including military time and a time display that will change with regional time zones,

3 0
3 years ago
Other questions:
  • What are some ways social media networks make money from users
    10·1 answer
  • Pls help............
    9·2 answers
  • A file to which information is written is referred to as a(n) ____ file.
    8·1 answer
  • Grace Hopper led the development of ______, a programming language for business applications.
    6·2 answers
  • Increasing your margins will increase the amount of space you have to type text on your document. Please select the best answer
    10·1 answer
  • What is the primary benefit of using solid state storage
    7·1 answer
  • Companies expose themselves to harsh sanctions by federal agencies when they violate the privacy policies that their customers r
    8·1 answer
  • Sara Beth and Taylor have developed a new software that they plan to distribute for free, allowing other software professionals
    7·1 answer
  • Problem 4 (25 points)Consider a byte addressing architecture with 64-bit memory addresses.(a)Which bits of the address would be
    5·1 answer
  • Which object event is an indication that something has been created but not committed into the database?
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!