Friday, 10 June 2022

Write a program to input a sentence. Convert in Upper case and shift each letter of the English alphabet two steps towards the right. Characters other than alphabets remain the same. Print original and new string in two different lines.

Sample Input: ABACUS12@RYZ
Sample Output: CDCEWU12@TAB


//ABACUS12@RYZ
import java.util.Scanner;
public class Assignment16
{
   public static void main(String args[]) 
   {
       int a,p,i;
       String st,str="";
       char chr;
       Scanner in = new Scanner(System.in);
       System.out.println("Enter a sentence:");
       st=in.next();
       p=st.length();       
        for(a=0;a<p;a++)
        {
            chr=st.charAt(a);
      
                if (chr>='A' && chr<='X')
                { i=chr;
                  i=i+2;
                  char c=(char)i;
                  str=str+c;
                } 
                else if (chr>'X' && chr<='Z')
                { i=chr;
                  i=i-24;
                  char c1=(char)i;
                  str=str+c1;
                }    
               else
               str=str+chr;
        }  
            System.out.print(str); 
}
}

No comments:

Post a Comment