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