Encrypt Sentence in Java using Several Parameters | AskTheCode

Java program that we can use to encrypt a sentence | Problem Solving in Java | AskTheCode


Design a Java program that we can use to encrypt a sentence by using the following methods respectively:

  1. Convert all letters to capital.

  2. reverseString: reverse the sentence.

  3. toNumbers: a method that converts the following letters to numbers:

"O" to zero (0), "S" to $, "L" to 1.

  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:


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.


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();
		// 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){
		return s;

58 views0 comments

Recent Posts

See All

Golf CodeChef Solution in Java - AskTheCode

CodeChef May Long Challenge Solution | Golf (LKDNGOLF) solution | AskTheCode Golf (LKDNGOLF) solution Problem: It's a lockdown. You’re bored in your house and are playing golf in the hallway. The hall

Solubility - CodeChef Solution in Java and C++| AskTheCode

CodeChef May Long Challenge Solution | Solubility (SOLBLTY) solution | AskTheCode Solubility (SOLBLTY) solution Problem: Suppose for a unit rise in temperature, the solubility of sugar in water increa

Valid Paths CodeChef Solution in C++ | AskTheCode

CodeChef May Long Challenge Solution | Valid Paths (VPATH) solution | AskTheCode Valid Paths (VPATH) CodeChef solution Problem: You are given a tree with N nodes numbered from 1 to N. A set S of nodes