I'm trying to create a Caesar Cipher program. While I am almost done, I have run into two major problems that I'm a little confused on how to fix. My Caesar Cipher works by finding the most common letter in the input. Because the most frequently used letter in English is 'e' my cipher shifts the values for each letter over by subracting the ASCII value of the most common element from the ASCII value of 'e'. While this works, I need my program to be able to find the second most frequent element used if the initial try isn't readable. If that doesn't work, the third, fourth, and so on. The next problem I'm having is that my output doesn't look very "clean". It's readable but interspersed with weird symbols.
maximum_character = unciphered_text[0]
maximum_count = unciphered_text.count(unciphered_text[0])
for char in unciphered_text:
if char is not " ":
if unciphered_text.count(char) > maximum_count:
maximum_character = char
print("The most frequent character used is: ", maximum_character)
ASCII_maximum = maximum_character.lower()
ASCII_number = ord(ASCII_maximum)
print(ASCII_number)
shift_distance = ord('e')-ASCII_number
print("The shift distance is: ", shift_distance)
def caesar_cipher(unciphered_text, shift_distance):
ciphered_text = ""
for char in unciphered_text:
cipher_process = ord(char)+shift_distance
post_translation = chr(cipher_process)
ciphered_text += post_translation
return ciphered_text
answer = caesar_cipher(unciphered_text, shift_distance)
print(answer)
This is what I mean by weird output.
Input:
Frzdugv glh pdqb wlphv ehiruh wkhlu ghdwkv; Wkh ydoldqw qhyhu wdvwh ri ghdwk exw rqfh. Ri doo wkh zrqghuv wkdw L bhw kdyh khdug, Lw vhhpv wr ph prvw vwudqjh wkdw phq vkrxog ihdu; Vhhlqj wkdw ghdwk, d qhfhvvdub hqg, Zloo frph zkhq lw zloo frph
Output:
Cowards
die
man_
times
before
their
deaths8
The
valiant
never
taste
of
death
but
once+
Of
all
the
wonders
that
I
_et
have
heard)
It
seems
to
me
most
strange
that
men
should
fear8
Seeing
that
death)
a
necessar_
end)
Will
come
when
it
will
come