I have an assignment to create a program that can decipher a Caesar Cipher that the user enters. The teacher provided us with a helper function:
double letterScore(char ch){
if (ch == 'A' || ch == 'a') return .0684;
if (ch == 'B' || ch == 'b') return .0139;
if (ch == 'C' || ch == 'c') return .0146;
if (ch == 'D' || ch == 'd') return .0456;
if (ch == 'E' || ch == 'e') return .1267;
if (ch == 'F' || ch == 'f') return .0234;
if (ch == 'G' || ch == 'g') return .0180;
if (ch == 'H' || ch == 'h') return .0701;
if (ch == 'I' || ch == 'i') return .0640;
if (ch == 'J' || ch == 'j') return .0033;
if (ch == 'K' || ch == 'k') return .0093;
if (ch == 'L' || ch == 'l') return .0450;
if (ch == 'M' || ch == 'm') return .0305;
if (ch == 'N' || ch == 'n') return .0631;
if (ch == 'O' || ch == 'o') return .0852;
if (ch == 'P' || ch == 'p') return .0136;
if (ch == 'Q' || ch == 'q') return .0004;
if (ch == 'R' || ch == 'r') return .0534;
if (ch == 'S' || ch == 's') return .0659;
if (ch == 'T' || ch == 't') return .0850;
if (ch == 'U' || ch == 'u') return .0325;
if (ch == 'V' || ch == 'v') return .0084;
if (ch == 'W' || ch == 'w') return .0271;
if (ch == 'X' || ch == 'x') return .0007;
if (ch == 'Y' || ch == 'y') return .0315;
if (ch == 'Z' || ch == 'z') return .0004;
return 0.0;
}
Apparently, these numbers are meant to be used for letter frequencies in the English language to determine what phrases are best to output as the deciphered phrase. For example, you'd expect that 12.67% of letters in a book are the letter "e", and so the return value for if the inputted letter is "e" is 0.1267.
That was the helper function. We are supposed to implement it inside another function, decipher
, which will have a string parameter str
. Decipher will be implemented 25 times within main and will decipher the string that the user inputs. The only issue is, I don't understand how I can use the helper function letterScore within decipher to discern how to decipher the Caesar Cipher given.