From f8630952258cb1c5a4c849ce9cd568ce91ed14b0 Mon Sep 17 00:00:00 2001 From: Nehir Date: Thu, 29 Jan 2026 23:52:52 +0300 Subject: [PATCH] Fixed cipher class' methods --- Cipher.java | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/Cipher.java b/Cipher.java index 7527c5b..d72befa 100644 --- a/Cipher.java +++ b/Cipher.java @@ -1,5 +1,5 @@ // This class is used for encrypting or decrypting strings using character mapping -public class Cipher +public class Cipher { // Strings for keeping the alphabets, one for the original letters and the other for the encrypted ones // encryption involves mapping from original to cipher, for each letter we locate the character in the @@ -13,9 +13,9 @@ public String encrypt(String inputString) { String outputString = ""; // for all chars in the input string - for (int i = 0; i < inputString.length(); i++) + for (int i = 0; i < inputString.length(); i++) { - + outputString += replaceChar(inputString.charAt(i), true); } return outputString; @@ -26,7 +26,10 @@ public String decrypt(String inputString) { // output string will be collected in this variable, one char at a time String outputString = ""; - replaceChar('a',true); + for (int i = 0; i < inputString.length(); i++) + { + outputString += replaceChar(inputString.charAt(i), false); + } return outputString; } @@ -39,15 +42,15 @@ public String decrypt(String inputString) { private char replaceChar(char inputChar, boolean isEncrypt) { if(isEncrypt) { - for (int i = 0; i < ORIGINAL_ALPHABET.length(); i++) + for (int i = 0; i < ORIGINAL_ALPHABET.length(); i++) { if(ORIGINAL_ALPHABET.charAt(i) == inputChar) { - + inputChar = CIPHER_ALPHABET.charAt(i); } } } else { - for (int i = 0; i < CIPHER_ALPHABET.length(); i++) + for (int i = 0; i < CIPHER_ALPHABET.length(); i++) { if(CIPHER_ALPHABET.charAt(i) == inputChar) { return ORIGINAL_ALPHABET.charAt(i); @@ -58,4 +61,4 @@ 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