Programming Examples

Java program to encode a string based on move


Write a program in java to accept a String and display the new string after encoding with move=2

Sample Input: "ZEOLOGY"

Sample Output: BGQNQIA

Solution:

import java.util.*;
class Encode
{
    public static void main(String arr[]) 
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter any String to be Encoded : ");
        String str=sc.nextLine();
        int i,c=0;
        char ch,chr=0;      
        System.out.println("Encoded : 2");
        System.out.println("Encoded String : ");
        for(i=0;i<str.length();i++)
        {
            ch=str.charAt(i);
            if((ch>='a' && ch<='x')||(ch>='A' && ch<='X'))            
                c=(int)(ch+2);
            chr=(char)c;
            if((ch>='y' && ch<='z')||(ch>='Y'&& ch<='Z'))
                c=(int)(ch-24);
            chr=(char)c;
            System.out.print(chr);
        }
    }
}
Output

Enter any String to be Encoded : 

COMPUTER

Encoded : 2

Encoded String : 

EQORWVGT

Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad