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

Latest Current Affairs 2025 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2025 Python Programming Tutorials 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 Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz