Write a program to accept a sentence from the user and encrypt it according the value given by the user.

for example if the user enters 2 all the letters in the string will be replaced with the character that comes after two letters in the alphabet.

Example:

Enter a Sentence
I love programming
Enter Shift Value
2
Original Sentence: I LOVE PROGRAMMING
Encrypted Sentence: K NQXG RTQITCOOKPI

import java.io.*;
class encryption{
    public static void main() throws IOException{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a Sentence");
        String s=br.readLine();
        s=s.toUpperCase();
        String str=" ";
        System.out.println("Enter Shift Value");
        int n=Integer.parseInt(br.readLine());
        String a[]=s.split(" ");
        for(int i=0;i<a.length;i++){
            for(int j=0;j<a[i].length();j++){
                char ch=a[i].charAt(j);
                if((ch>(65-n)&&ch<65||ch>(90-n)&&ch<90)){
                    str+=(char)(ch-(26-n));
                }
                else{
                    str+=(char)(ch+n);
                }
            }
                str+=" ";
            }
            System.out.println("Original Sentence: "+s);
            System.out.println("Encrypted Sentence: "+str);
            System.out.println();
        }
    }
SHARE
Previous articleTwin Prime
Next articleHappy Number

LEAVE A REPLY

Please enter your comment!
Please enter your name here