From bba6562ce09102b3634b4590baf119ad80a0dd16 Mon Sep 17 00:00:00 2001 From: Yusuf Mert Deniz Date: Wed, 25 Sep 2024 15:33:29 +0300 Subject: [PATCH] Update Update --- Cipher.java | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/Cipher.java b/Cipher.java index 7527c5b..80d686f 100644 --- a/Cipher.java +++ b/Cipher.java @@ -8,24 +8,31 @@ public class Cipher public static final String CIPHER_ALPHABET = "dfxyhrklvwuasgimnojpqetbcz"; public String encrypt(String inputString) { - + // output string will be collected in this variable, one char at a time String outputString = ""; - + // for all chars in the input string for (int i = 0; i < inputString.length(); i++) { - + // append the encrypted version of the char to the output string + outputString += replaceChar(inputString.charAt(i), true); } return outputString; } public String decrypt(String inputString) { - + // output string will be collected in this variable, one char at a time String outputString = ""; - + + // for all chars in the input string + for (int i = 0; i < inputString.length(); i++) + { + // append the encrypted version of the char to the output string + outputString += replaceChar(inputString.charAt(i), false); + } replaceChar('a',true); return outputString; @@ -37,12 +44,12 @@ public String decrypt(String inputString) { // works only when the input char is included in our alphabet variables // should not replace symbols or upper case letters, return input char in those cases private char replaceChar(char inputChar, boolean isEncrypt) { - + if(isEncrypt) { for (int i = 0; i < ORIGINAL_ALPHABET.length(); i++) { if(ORIGINAL_ALPHABET.charAt(i) == inputChar) { - + return CIPHER_ALPHABET.charAt(i); } } } @@ -54,8 +61,9 @@ private char replaceChar(char inputChar, boolean isEncrypt) { } } } - + // if we did not find it in the alphabet, then return the original char return inputChar; } -} \ No newline at end of file +} + \ No newline at end of file