From ab003b062a3d85c71ec3e42b70dd6297ade722f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eralp=20Yi=C4=9Fit=20Boz?= Date: Wed, 24 Sep 2025 21:49:26 +0300 Subject: [PATCH] I fixed the encrypt, descrypt and replaceChar metod --- Cipher.java | 60 +++++++++++++++++++++++++++++------------------------ 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/Cipher.java b/Cipher.java index 7527c5b..5c83ce2 100644 --- a/Cipher.java +++ b/Cipher.java @@ -1,33 +1,41 @@ // This class is used for encrypting or decrypting strings using character mapping -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 - // original string and replace it with the cipher alphabet letter at the same position +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 + // original string and replace it with the cipher alphabet letter at the same + // position public static final String ORIGINAL_ALPHABET = "abcdefghijklmnopqrstuvwxyz"; - public static final String CIPHER_ALPHABET = "dfxyhrklvwuasgimnojpqetbcz"; + 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++) - { - } + // for all chars in the input string + for (int i = 0; i < inputString.length(); i++) { + char ch = inputString.charAt(i); + + outputString += replaceChar(ch, true); + } return outputString; } 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++) { + char x = inputString.charAt(i); + + outputString += replaceChar(x, false); + } + return outputString; } @@ -35,27 +43,25 @@ public String decrypt(String inputString) { // if isEncrypt == true -> original to encrypted // 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 + // 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) { + if (isEncrypt) { + for (int i = 0; i < ORIGINAL_ALPHABET.length(); i++) { + if (ORIGINAL_ALPHABET.charAt(i) == inputChar) { + return CIPHER_ALPHABET.charAt(i); } } - } - else { - for (int i = 0; i < CIPHER_ALPHABET.length(); i++) - { - if(CIPHER_ALPHABET.charAt(i) == inputChar) { + } else { + for (int i = 0; i < CIPHER_ALPHABET.length(); i++) { + if (CIPHER_ALPHABET.charAt(i) == inputChar) { return ORIGINAL_ALPHABET.charAt(i); } } } - + // 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