From b73d60a6127cc9b907c89f256d92cc9917630555 Mon Sep 17 00:00:00 2001 From: AhmetSerhan <159904294+AhmetSerhan25@users.noreply.github.com> Date: Wed, 24 Sep 2025 16:08:12 +0300 Subject: [PATCH] Update Cipher.java Several errors fixed. --- Cipher.java | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/Cipher.java b/Cipher.java index 7527c5b..372021d 100644 --- a/Cipher.java +++ b/Cipher.java @@ -8,13 +8,15 @@ 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); } @@ -22,10 +24,17 @@ public String encrypt(String inputString) { } 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,11 +46,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 +64,8 @@ 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 +}