Hacker Rank Practice solution - Ciphering and Deciphering

Hacker Rank questions and solution:


1. Ciphering and Deciphering(Passed all the test cases)

Ans: Adding only the methods and it's solution for below 4 steps:

        a) To change the given string to uppercase from lowercase and vise-versa.

        b) To reverse the string using StringBuilder

        c) To convert the even place letters with corresponding ascii value

        d) To replace all the space with "*";

        e) Return the ciphered string.

        d) For Deciphering reverse the above step.

        

Solution: 


public class CipherDecipher {


public String ciphering(String normal){

//converted String to array
char[] caseSwap = normal.toCharArray();

//converted upper to lower and vise-versa
for(int i = 0 ; i <caseSwap.length; i++){
if(Character.isDigit(caseSwap[i])){
return null;
}
if(caseSwap[i]>='a' && caseSwap[i] <='z'){
caseSwap[i]-=32;
} else if(caseSwap[i]>='A' && caseSwap[i] <='Z'){
caseSwap[i]+=32;
}
}

//reverse
// System.out.println(new String(caseSwap));
normal = new String(caseSwap);
normal = (new StringBuilder(normal).reverse().toString());

//replace
normal = normal.replace(" ", "*");

//character to ascii value
char[] asciiValue = normal.toCharArray();
String finalValue="";
for(int i = 0; i<asciiValue.length ; i++){
if(i%2==1) {
// finalValue += Integer.toString((int) asciiValue[i]);
finalValue += String.valueOf((int) asciiValue[i]);
System.out.println("final value of toString "+ finalValue);
} else {
finalValue += asciiValue[i];
}
}

//added 3 to the value
finalValue += "3";
return finalValue;
}







public String deciphering(String ciphered){
char[] v1 = ciphered.toCharArray();

//3 removed. can also be solved using substring method of String class.
String removeThree = "";
for(int i = 0; i< v1.length-1; i++) {
removeThree += v1[i];
}
char[] asciiValue = removeThree.toCharArray();


//converting the ascii to letters
String finalWord ="";
String temp = "";
int comparision = 0;
for(int i = 0; i< asciiValue.length; i++){
if(Character.isDigit(asciiValue[i])){
temp = temp+asciiValue[i];
if((temp.length()>=2 && temp.length()<=3)){
comparision = Integer.parseInt(temp);
} else continue;

if((comparision>=32 && comparision <=126)){
char correspondingCharValue = (char) comparision;
finalWord += correspondingCharValue;
}

} else {
finalWord += asciiValue[i];
temp = "";
}
}


//replace
finalWord = finalWord.replace("*", " ");


//reverse
finalWord = (new StringBuilder(finalWord).reverse().toString());

//upper to lower case and vise-versa
char[] caseSwap = finalWord.toCharArray();
for(int i = 0 ; i <caseSwap.length; i++){
if(caseSwap[i]>='a' && caseSwap[i] <='z'){
caseSwap[i]-=32;
} else if(caseSwap[i]>='A' && caseSwap[i] <='Z'){
caseSwap[i]+=32;
}
}

finalWord = new String(caseSwap);

return finalWord;
}

No comments:

Post a Comment

Linux OS - SSH configuration

 SSH connection using Putty: 1. sudo apt-get update 2. sudo apt-get upgrade 3. sudo apt remove openssh-server 4. sudo apt install openssh-se...