You are viewing a single comment's thread. Return to all comments →
C#
const int AlphabetLength = 26; public static string caesarCipher(string s, int k) { var cipher = s.ToCharArray(); int n = s.Length; for (int i = 0; i < n; i++) { if (isLowerAlphaNumeric(s[i])) cipher[i] = getShiftedLowerAlphabetBy(s[i], k); else if (isUpperAlphaNumeric(s[i])) cipher[i] = getShiftedUpperAlphabetBy(s[i], k); } return new String(cipher); } private static bool isLowerAlphaNumeric(char c) => isCharWithinAlphabetStartingAt(c, 'a'); private static bool isUpperAlphaNumeric(char c) => isCharWithinAlphabetStartingAt(c, 'A'); private static bool isCharWithinAlphabetStartingAt(char c, char startingPoint) => c >= startingPoint && c < startingPoint + AlphabetLength; private static char getShiftedLowerAlphabetBy(char c, int leftShift) => getShiftedAlphabetStartingAt(c, 'a', leftShift); private static char getShiftedUpperAlphabetBy(char c, int leftShift) => getShiftedAlphabetStartingAt(c, 'A', leftShift); private static char getShiftedAlphabetStartingAt(char c, char startingPoint, int leftShift) => (char) (startingPoint + (c - startingPoint + leftShift) % AlphabetLength);
Seems like cookies are disabled on this browser, please enable them to open this website
Caesar Cipher
You are viewing a single comment's thread. Return to all comments →
C#