0

I am having trouble with my Caesar code wrapping around the alphabet on large number keys. For one example, I would insert message barfoo with a key of 25 and have an output not what would be expected. I've look at every question about this on here and it hasn't really helped my understanding of how to convert the ascii values to the alphabet index. How can I correct this error? Thank you, Joe

5

First of all you have to find the index in the alphabet of each letter in your plaintext. So if your plaintext is "abce" your indexes should be [0, 1, 2, 4] as

  • A/a is 0
  • B/b is 1
  • ...
  • Z/z is 25

You should use the ASCII table to find in what position of the table is each letter, and then subtract 'A' or 'a' to get the right index.

Then to wrap around, you have to use the modulo operator, wich gives you the remainder of an integer division. So 3 % 2 == 1 and 6 % 4 == 2. Keeping in mind that the latin alphabet has 26 letters, you must find a way to use modulo to keep the index inside the limits [0, 25].

For example if the space was [0, 5] you would do i % 6 because:

  • 0 % 6 == 0
  • 1 % 6 == 1
  • 2 % 6 == 2
  • 3 % 6 == 3
  • 4 % 6 == 4
  • 5 % 6 == 5
  • 6 % 6 == 0
  • 7 % 6 == 1
  • ...

See how it wraps around? That should get you going. If this answers your question please accept it by clicking the gray check-mark to the left, so that it becomes green. You can also vote it up by pressing the up arrow above the check-mark.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .