[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Question 3:

Caesar Cipher is an encryption technique which is implemented as ROT13 (‘rotate by 13 places’). It is a simple letter substitution cipher that replaces a letter with the letter 13 places after it in the alphabets, with the other characters remaining unchanged.
Write a program to accept a plain text of length L, where L must be greater than 3 and less than 100.
Encrypt the text if valid as per the Caesar Cipher.
Test your program with the sample data and some random data:
Example 1
INPUT : Hello! How are you?
OUTPUT : The cipher text is:
Uryyb? Ubj ner lbh?
Example 2
INPUT : Encryption helps to secure data.
OUTPUT : The cipher text is:
Rapelcgvba urycf gb frpher qngn.
Example 3
INPUT : You
OUTPUT : INVALID LENGTH

SOURCE CODE  


import java.io.*;
import java.util.*;
public class ISC2017_CaesarCipher
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter a String    :");
        String str=sc.nextLine();
        String st="";
        int l=str.length();
        if(l>3&&l<100)
        {
            for(int i=0;i<l;i++)
            {
                char ch=str.charAt(i);
                if(ch>='A'&&ch<='M'||ch>='a'&&ch<='m')
                {
                    st=st+ (char)(ch+13);
                }
                else if(ch>='N'&&ch<='Z'||ch>='n'&&ch<='z')
                {
                    st=st+ (char)(ch-13);
                }
                else
                {
                    st=st+ch;
                }
            }
            System.out.println("\nThe cipher text is   :\n"+st);
        }
        else
        {
            System.out.println("Invalid Length");
        }
    }
}


OUTPUT





If you have any question then leave a comment below I will do my best to answer that question.


Comments

Post a Comment

Popular posts from this blog

Pattern of your own name.

Designing Patterns - Print 'Z'.