Skip to content

Updated Cipher.java by filling out the missing parts in the code #537

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 21 additions & 18 deletions Cipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public String encrypt(String inputString) {
// for all chars in the input string
for (int i = 0; i < inputString.length(); i++)
{

char currentCharacter = inputString.charAt(i);
outputString += replaceChar(currentCharacter, true);
}

return outputString;
Expand All @@ -26,7 +27,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 k = 0; k < inputString.length(); k++){
char currentCharacter = inputString.charAt(k);
outputString += replaceChar(currentCharacter, false);
}

return outputString;
}
Expand All @@ -37,25 +41,24 @@ 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) {

}
int characterIndex = ORIGINAL_ALPHABET.indexOf(inputChar);
if(characterIndex != -1){
return CIPHER_ALPHABET.charAt(characterIndex);
}
}
else {
for (int i = 0; i < CIPHER_ALPHABET.length(); i++)
{
if(CIPHER_ALPHABET.charAt(i) == inputChar) {
return ORIGINAL_ALPHABET.charAt(i);
}
else{
return inputChar;
}
}

// if we did not find it in the alphabet, then return the original char
return inputChar;
else {
int characterIndex = CIPHER_ALPHABET.indexOf(inputChar);
if (characterIndex != -1){
return ORIGINAL_ALPHABET.charAt(characterIndex);
}
else {
return inputChar;
}
}
}
}
}