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

I don`t expect someone to solve it for me but if you could give any correct help

ID: 3547347 • Letter: I

Question

I don`t expect someone to solve it for me but if you could give any correct help it would be appreciated and shoud get you some points. Mostly, I need help doing the rotate_letter function as I have no idea what to do with it. I think my cesear_cipher function is right as it is basically a switch that calls either the rotate_left or rotate_right function but I`m not 100% sure.

Problem:  The Caesar Cipher is a simple letter substitution cipher (cipher: algorithm for performing encryption or decryption) that was developed in ancient Rome and used by Julius Caesar to communicate to his army.  ROT13 is one instance of this cipher, and it replaces a letter with the letter 13 letters after it in the alphabet:

A

Explanation / Answer

Here's the idea of how it's done. I'm not too sure with the instructions what you are expected to do with the alphabet array actually. I just had it there since it was asked to be passed and used in a manner where i don't think it is even neccesary, Hopefully this provides a decent amount of information for you to advance in your program.


#include <iostream>
#include <cstring>
using namespace std;

void initAlpha(char alphabet[26])
{
    for(int i = 0; i < 26; i++){
          alphabet[i] = 'a' + i;
    }    
}

char rotate_letter(char letter, int rotation, const char alpha[26])
{
     //rotate the letter along the alphabet array
     //letter-97 gives the index of where the letter is on the alphabet array
    
     //if the rotation added is past 'z' it needs to be back in range by subtracting 26
     if( (alpha[letter-97]+rotation) > 'z' ){
          letter = alpha[letter-97] + rotation - 26;
     }
     else{
          letter = alpha[letter-97] + rotation;
     }
     return letter;
}    

void rotate_right(char message[201], int rotation, const char alpha[26]){
     //gets rid of looping the alphabet array multiple times
     rotation = rotation%26;
     int length = strlen(message);
     for(int i = 0; i < length; i++){
        //skips the rotation of that index if index is not an alphabetic character
        if( message[i] < 'a' || message[i] > 'z'){
            continue;
        }
        else{
             message[i] = rotate_letter(message[i],rotation, alpha);
        }
     }
}

void rotate_left(char message[201], int rotation, const char alpha[26]){
     //gets rid of looping the alphabet array multiple times
     rotation = rotation%26;
     //rotating left is the same as rotating right by 26-rotation
     rotation = 26 - rotation;
     int length = strlen(message);
     for(int i = 0; i < length; i++){
        //skips the rotation of that index if index is not an alphabetic character
        if( message[i] < 'a' || message[i] > 'z'){
            continue;
        }
        else{
             message[i] = rotate_letter(message[i],rotation, alpha);
        }
     }
}

int main()
{
    char msg[201];
    char alphabet[26];
    int rotation;
    initAlpha(alphabet);
    while(1)
    {
            cin.getline(msg,200);
            cout << msg << endl;
            rotate_right(msg, 13, alphabet);
            cout << msg << endl;
            cin.getline(msg,200);
            rotate_left(msg, 13, alphabet);
            cout << msg << endl;
           
    }
    return 0;
}