Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Caesar cipher is a simple and widely known encryption technique. The action

ID: 3586834 • Letter: T

Question

The Caesar cipher is a simple and widely known encryption technique. The action of a Caesar cipher is to replace each letter in the unencrypted message (called "plaintext" in cryptography) with a different one a fixed number of places down the alphabet. The resulting message is called "ciphertext". For example:

Plaintext: Hello, world!
Ciphertext: Ebiil, tloia!

The cipher illustrated above uses a left shift of three (or a right shift of 23), so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext.

Write a C++ function to implement the Caesar cipher, using the prototype:

   precond: numpos>=0 and rshift <=26
     postcond: the return value is the encrypted version of orig using the caesar(rshift) cipher
     string enc rypt Caesar(string orig , int rshift );

Your function should preserve case, and any non-alphabetic characters should be left unchanged. For example, encryptCaesar("Hello, world!",23) = "Ebiil, tloia!".

Explanation / Answer

#include <bits/stdc++.h>
using namespace std;

string encryptCeaser(string str, int rshift)
{
int i,j;
string s="";
char c;
for(i=0;i<str.size();i++)
{
if(str[i]>='a' && str[i]<='z')
{
j=(str[i]-'a'+rshift+26)%26;
c=j+97;
s+=c;
}
else if(str[i]>='A' && str[i]<='Z')
{
j=(str[i]-'A'+rshift+26)%26;
c=j+65;
s+=c;
}
else
s+=str[i];
}
return s;
}

int main()
{
cout<<encryptCeaser("Hello, world!",23)<<endl;
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote