From 23a8b881de953abd33856c80b712e4e866912d18 Mon Sep 17 00:00:00 2001 From: Bilal <159887974+BilalBerkKacar@users.noreply.github.com> Date: Fri, 27 Sep 2024 22:19:27 +0300 Subject: [PATCH] Update Cipher.java errors are fixed --- Cipher.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Cipher.java b/Cipher.java index 7527c5b..634b80e 100644 --- a/Cipher.java +++ b/Cipher.java @@ -7,7 +7,7 @@ public class Cipher public static final String ORIGINAL_ALPHABET = "abcdefghijklmnopqrstuvwxyz"; public static final String CIPHER_ALPHABET = "dfxyhrklvwuasgimnojpqetbcz"; - public String encrypt(String inputString) { + public static String encrypt(String inputString) { // output string will be collected in this variable, one char at a time String outputString = ""; @@ -15,19 +15,19 @@ public String encrypt(String inputString) { // for all chars in the input string for (int i = 0; i < inputString.length(); i++) { - + outputString +=replaceChar(inputString.charAt(i),true); } return outputString; } - public String decrypt(String inputString) { + public static 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; } @@ -36,17 +36,17 @@ public String decrypt(String inputString) { // if isEncrypt == false -> encrypted to original // 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) { + public static char replaceChar(char inputChar, boolean isEncrypt) { - if(isEncrypt) { + if(isEncrypt && Character.isLowerCase(inputChar)) { for (int i = 0; i < ORIGINAL_ALPHABET.length(); i++) { if(ORIGINAL_ALPHABET.charAt(i) == inputChar) { - + return CIPHER_ALPHABET.charAt(i); } } } - else { + else if(!isEncrypt && Character.isLowerCase(inputChar)){ for (int i = 0; i < CIPHER_ALPHABET.length(); i++) { if(CIPHER_ALPHABET.charAt(i) == inputChar) { @@ -58,4 +58,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 +}