icse-promo

Question

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

                                                    ROT13

A/a	B/b	C/c	D/d	E/e	F/f	G/g	H/h	I/i	J/j	K/k	L/l	M/m
↕	↕	↕	↕	↕	↕	↕	↕	↕	↕	↕	↕	↕
N/n	O/o	P/p	Q/q	R/r	S/s	T/t	U/u	V/v	W/w	X/x	Y/y	Z/z
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class Caesar
{
    public static void main(String args[])
    {
        int i=0,len=0;
        char ch=' ';
        String sen="",cipher="";
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter text: ");
        sen = sc.nextLine();
        len = sen.length();
        if(len < 4 || len > 99)
        {
            System.out.println("INVALID LENGTH");
            return;
        }
        for(i = 0; i < len; i++)
        {
            ch = sen.charAt(i);
            if(Character.isLetter(ch))
            {
                
                    if(ch > 'M'||ch>'m')
                    {
                        cipher += (char)(ch - 13);
                    }
                    else
                    {
                        cipher += (char)(ch + 13);
                    }
                
            }
            else
            {
                cipher += ch;
            }
        }
        System.out.println("The cipher text is:\n" + cipher);
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *