I am completely new to the programming world and I am having trouble writing this code correctly. I am supposed to shift the letters of the alphabet 5 spaces to the right. I have come up with two equations. if c<(u) ascii(117) then I simply add 5. but if c>= v(ascii 118) then I subtract 21. I am trying to write this in if statements. thank you in advance!
That said, if you want to implement the Caesar cipher a more elegant approach is to not rely on ASCII codes at all. Instead do what would be done in real life: use an alphabet and shift it.
OA: Original Alphabet
SA: Shifted Alphabet (in this example, shifted by 5)
OA: abcdefghijklmnopqrstuvwxyz
SA: fghijklmnopqrstuvwxyzabcde
After we construct the Shifted Alphabet, all we have to do is: 1) go through the Message one letter at a time 2) find the position P of the letter in the Original Alphabet 3) replace the letter in the Message with the letter at position P in the Shifted Alphabet
OM: Original Message
EM: Encrypted Message
0 1 2
01234567890123456789012345 (counting from 0 to 25)
OA: abcdefghijklmnopqrstuvwxyz
SA: fghijklmnopqrstuvwxyzabcde
OM: hello
EM: ?????
read letter in OM: h
in OA, letter h is at position 7
in SA, at position 7 is letter m
write letter in EM: m
and so on, until we complete EM:
EM: mjqqt
Creating the Original Alphabet and Shifted Alphabet are both very easy to do:
1 2 3 4 5 6 7 8 9
#include <algorithm>
#include <string>
// ...
std::string OA("abcdefghijklmnopqrstuvwxyz");
std::string SA(OA); // first copy the OA
std::rotate(SA.begin(), SA.begin() + 5, SA.end());
#include <iostream>
usingnamespace std ;
main()
{
int myinput;// enter value
int shift= 13; // shift alphabet 13 to the right
myinput = cin.get();
while (! cin.eof())
{
if (myinput == 32)
myinput = myinput ;// input spaces, output spaces
elseif (myinput >=97 and myinput<110)//letter is between 'a' and 'n'
myinput+=shift;// if letter is less than 'n' shift 13 spaces
elseif (myinput<= 122 and myinput >= 110)//letter is between 'n' and 'z'
myinput-= shift ;// if letter is greater than or equal to 'n', subtract 13 spaces
elseif (myinput>= 65 and myinput<78)//letter is between 'A' and 'N'
myinput+=shift;//if letter is less than 'N' shift 13 spaces
elseif (myinput<=90 and myinput >= 78)//letter is between 'N' and 'Z'
myinput-=shift ;//if letter is greater than or equal to 'N', subtract 13 spaces
cout.put(myinput);// put 'myinput' into program
myinput= cin.get();
}
myinput=cin.get();
}
Another tip you can make it more readable if you change your if statements from the ascii code to the actual characters.
1 2 3 4 5 6 7 8 9 10 11
if (myinput == ' ')
myinput = myinput ;// input spaces, output spaces
elseif (myinput >= 'a' && myinput< 'n')//letter is between 'a' and 'n'
myinput+=shift;// if letter is less than 'n' shift 13 spaces
elseif (myinput<= 'n' && myinput >= 'z')//letter is between 'n' and 'z'
myinput-= shift ;// if letter is greater than or equal to 'n', subtract 13 spaces
elseif (myinput>='A' && myinput< 'N')//letter is between 'A' and 'N'
myinput+=shift;//if letter is less than 'N' shift 13 spaces
elseif (myinput<= 'N' && myinput >= 'Z')//letter is between 'N' and 'Z'
myinput-=shift ;//if letter is greater than or equal to 'N', subtract 13 spaces
Another thing to mention is you can do this with 3 if's instead of 5 if you combine the a-n with A-N and the n-z with N-Z. Since they both do the same thing.