Write a program to accept a sentence and then convert each character to second next character

 

Q Write a program to accept a sentence and then convert each character to second next character.

                The character A becomes C, Y becomes Z & Z becomes B.

SOLUTION:

import java.lang.String;
import java.util.Scanner;
class q8
{
    public static void main(String args[])
    {
        String str;
        String str2 = new String();
        int i = 0;
        char c;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter the Sentence: ");
        str = sc.nextLine();
        while(i < str.length())
        {
            c = str.charAt(i);
            if(c != ' ')
                if(c == 'y')
                    c = 'a';
                else if(c == 'z')
                    c = 'b';
                else if(c == 'Y')
                    c = 'A';
                else if(c == 'Z')
                    c = 'B';
                else
                    c = (char)((int)c + 2);
            str2 = str2.concat(String.valueOf(c));
            i++;
        }
        System.out.println(str2);
    }
}

OUTPUT:







Comments