ROT 13 Program in JAVA

ROT 13 Program in JAVA

 ROT 13 means ROTATE 13. It is an Encryption Scheme which works by cyclic shifting of each Lower Case and Upper Case letter 13 position.

Example :- 
Input - Encryption
Output - Rapelcgvba

Algorithm :-

Logic - Divide total alphabets into 2 halves i.e. first half is A-M and Second half is N-Z. ( Same for lower case also).

Step 1 - Input a word in a variable and find its length.
Step 2 - Extract each character and convert it into its ASCII code.
Step 3 - If the character is in the first half then increment the value of ASCII code by 13. If the character is in the second half then decrease the value of ASCII code by 13.
Step 4 - convert the new ASCII code into character in a new variable.
Step 5 - Store the new character in new string variable. (Say this word is Encrypted word)
Step 6 - Display both Original and Encrypted word.


Program :-

import java.io.*;
import java.util.*;
class Encrypt
{
    public static void main(String args[])
    {

        Scanner in= new Scanner(System.in);
        String str,str1= ""; // string variables to store words
        char ch,ch1; // character variables
        int i,y,l; // 'i' for loop , 'y' to convert letter into integer
        System.out.println("Enter Message");
        str= in.nextLine(); // storing original word
        l= str.length(); // storing length of the word
        for(i=0;i<l;i++)
        {
            ch= str.charAt(i); // extracting character of the string
            y = (int) ch; // converting character into its ASCII code
            if(y>=65&&y<78) // ASCII Code between "A - M"
            {
                y= y+13; // Increasing the letter by 13
                ch1= (char)y; // converting new ASCII code to Character
                str1= str1+ch1; // Storing new Character in new string
            }
            else if(y>=78&&y<91) // ASCII Code between "N - Z"
            {
                y= y-13; // Decreasing the letter by 13
                ch1= (char)y; // converting new ASCII code to Character
                str1= str1+ch1; // Storing new Character in new string
            }
            else if(y>=97&&y<110) // ASCII Code between "a - m"
            {
                y= y+13; // Increasing the letter by 13
                ch1= (char)y; // converting new ASCII code to Character
                str1= str1+ch1; // Storing new Character in new string
            }
            else if(y>=110&&y<123)
            {

                y= y-13; // Decreasing the letter by 13
                ch1= (char)y; // converting new ASCII code to Character
                str1= str1+ch1; // Storing new Character in new string
            }
        }

        System.out.println("Original Message : " +str); // Printing Original String
        System.out.println("Encrypted Message : " +str1); // Printing Encrypted String
    } // End of Main
} // End of Class


Note : There is an alternative method of this program which is 
easier. Checkout the link below.

Link : https://bluejcode.blogspot.com/2019/05/rot13-program-easy-method.html

All the Best :)
Keep Learning :)

Comments

Popular posts from this blog

Frequency of each digit of a number in Java

Trimorphic Number in JAVA

PalPrime Number in JAVA