Caesar cipher +5

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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std ;
main()
{
 int c;
 int shift= 5;
 {
     if (c + 5 > 122 )
        c = (c+ shift);
     else if(c == 32)
        c = c;
     else if (c < 117)
        c= (c + 5);
     else if c >= 118)
        c= (c- 21);
     else if (c== '\0')
              break;
     else
        c= c + shift;
 }
  
The ASCII table is available here if you need it:
http://www.cplusplus.com/doc/ascii/

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());


http://www.cplusplus.com/reference/algorithm/rotate/

Here's an online tool you can use to test that your algorithm is correct:
http://www.simonsingh.net/The_Black_Chamber/caesar.html
Last edited on
Thank you very much!
I did it both ways and I got the concept and the practice down!
blue10 wrote:
I did it both ways and I got the concept and the practice down!

If you would like help with refining your programs, please post your code for others to review.
Here is my finished code for this project



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
using namespace 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
    else if (myinput >=97 and myinput<110)//letter is between 'a' and 'n'
        myinput+=shift;// if letter is less than 'n' shift 13 spaces
    else if (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

    else if (myinput>= 65 and myinput<78)//letter is between 'A' and 'N'
        myinput+=shift;//if letter is less than 'N' shift 13 spaces
    else if (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
    else if (myinput >= 'a' && myinput< 'n')//letter is between 'a' and 'n'
        myinput+=shift;// if letter is less than 'n' shift 13 spaces
    else if (myinput<= 'n' && myinput >= 'z')//letter is between 'n' and 'z'
         myinput-= shift ;// if letter is greater than or equal to 'n', subtract 13 spaces

    else if (myinput>='A' && myinput< 'N')//letter is between 'A' and 'N'
        myinput+=shift;//if letter is less than 'N' shift 13 spaces
    else if (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.
Topic archived. No new replies allowed.