Java program that we can use to encrypt a sentence | Problem Solving in Java | AskTheCode
Problem:
Design a Java program that we can use to encrypt a sentence by using the following methods respectively:
Convert all letters to capital.
reverseString: reverse the sentence.
toNumbers: a method that converts the following letters to numbers:
"O" to zero (0), "S" to $, "L" to 1.
beginAndEnd: that add ** at the beginning and at the end of the sentence.
Finally, you should display the encrypted sentence.
Hint: use StringBuilder.
Sample input:
Welcome to AskTheCode - a community for providing support to coders.
Sample output:
**.$RED0C 0T TR0PPU$ GNIDIV0RP R0F YTINUMM0C A - ED0CEHTK$A 0T EM0C1EW**
Note: We can do the encryption using other classes, and could perform the implementation by other means too, as mention to use StringBuilder (in hint); so we are using String Builder class to perform the encryption.
Code:
import java.io.*;
import java.util.*;
class EncryptSentence{
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a string to encrypt: ");
String str_input = br.readLine();
System.out.println("The encrypted sentence is:\n"+encrypt(str_input));
}
// Method that calls other required methods for encryption
private static String encrypt(String str) throws StringIndexOutOfBoundsException{
StringBuilder s = new StringBuilder();
s.append(str.toUpperCase());
// Reversing the sentence
s = reverseString(s);
// Performing the conversion
s = toNumbers(s);
// Adding '**' at the beginning and at the ending
s = beginAndEnd(s);
return s.toString();
}
// Method which returns the sentence in reversed order
private static StringBuilder reverseString(StringBuilder s){
return s.reverse();
}
// Method which is used to convert 'O' to zero, 'S' to $, and 'L' to 1
private static StringBuilder toNumbers(StringBuilder s){
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'O') {
s.setCharAt(i, '0');
}
if (s.charAt(i) == 'S') {
s.setCharAt(i, '$');
}
if (s.charAt(i) == 'L') {
s.setCharAt(i, '1');
}
}
return s;
}
// Method to add '**' at the beginning and at the end
private static StringBuilder beginAndEnd(StringBuilder s){
s.insert(0,"**");
s.insert(s.length(),"**");
return s;
}
}
Comments