Computer Science, asked by Anonymous, 3 months ago

In java, a string class is considered a reference type.

Write a java program to cipher and decipher a message. Cipher is a system of writing that prevents unintended targets (users) from understanding a message.

Create a class "CipherDecipher" and write two methods public String ciphering(String normal)(logic for ciphering), and public String deciphering(String ciphered)(logic for deciphering).

To cipher a message:
1. Swap the case of the string-Upper Case to Lower Case letters and vice-versa.
2. Reverse the string using StringBuilder.
3. Replace the spacing between characters in the string with *(star).
4. Replace the character in even positions with their corresponding ASCII value.
5. Append any integer to the String.

The final string is the ciphered message.

To decipher the message, do the above steps in reverse order.

Input:
The first line contains the string to cipher.
The second line contains the ciphered text of the second input.
Output:
The first line contains the ciphered text of the first input.
The second line contains the ciphered text of the second input.

Sample input:
Welcome to hackerrank
?85O89*69R65*87O104*33I1043
Sample output:
K78A82R69K67A72*79T42E77O67L69w3
Hi! How are you?

Answers

Answered by pragyakirti12345
2

Answer: Java program is given below.

Explanation:

import java.io.*;

import java.math.*;

import java.security.*;

import java.text.*;

import java.util.*;

import java.util.concurrent.*;

import java.util.function.*;

import java.util.regex.*;

import java.util.stream.*;

import static java.util.stream.Collectors.joining;

import static java.util.stream.Collectors.toList;

class CipherDecipher{

       public String swapCase(String str){

       char [] ch =str.toCharArray();

       for(int i=0;i<str.length();i++){

           if(ch[i]<=90&&ch[i]>=65){

               ch[i]+=32;

           }

           else if(ch[i]<=122&&ch[i]>=97)

               ch[i]-=32;

       }

       return String.valueOf(ch);

   }

   

   public String asciiString(String str){

       StringBuilder sb=new StringBuilder();

       int n =str.length();

       for (int i=0;i<n;i++){

           if(i%2==1){

               sb.append((int)str.charAt(i));

           

           }

           else

               sb.append(str.charAt(i));

           

       }

       return sb.toString();

   }

   

   public String toChar(String str){

       StringBuilder sb=new StringBuilder();

       int n=str.length();

       for (int i=0;i<n-1;i++){

           char c=str.charAt(i);

           if(Character.isDigit(c)){

               int ch=c-'0';

               while(i<n-2&&Character.isDigit(str.charAt(++i))){

                   ch*=10;

                   ch+=str.charAt(i)-'0';

                   

               }

               c=(char)ch;

               sb.append(c);

               if(!Character.isDigit(str.charAt(i)))

               sb.append(str.charAt(i));

               continue;

               

           }

           sb.append(c);

           

       }

       return sb.toString();

       

   }

   

   public String ciphering(String normal){

       

       normal=swapCase(normal);

       normal=(new StringBuilder(normal)).reverse().toString();

       normal=normal.replaceAll(" ","*");

       normal=asciiString(normal);

       normal=normal+"3";

       

       return normal;

   }

   

   

   public String deciphering(String ciphered){

       ciphered=toChar(ciphered);

       ciphered=ciphered.replaceAll("\\*"," ");

       ciphered=(new StringBuilder(ciphered)).reverse().toString();

       ciphered=swapCase(ciphered);

       return ciphered;

 

   }

}

public class Solution {

   

   public static void main(String[] args){

       Scanner readInput = new Scanner(System.in);

       String normal=readInput.nextLine();

       String ciphered=readInput.nextLine();

       

       CipherDecipher cipherOrDecipher = new CipherDecipher();

       System.out.println(cipherOrDecipher.ciphering(normal));

       System.out.println(cipherOrDecipher.deciphering(ciphered));

   }  

}

__________________________________________________

Related Links :

Write a Java program to take the marks of students from roll numbers 0 to 4 and store them in an array. After storing the marks, print the marks of the student with roll number 4.​

https://brainly.in/question/39423796

Java program uses a compiler as well as an interpreter explain.

https://brainly.in/question/4940831

#SPJ2

Similar questions