KnowledgeBoat Logo

Computer Applications

Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.

Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024

Java

Java String Handling

ICSE Sp 2025

5 Likes

Answer

import java.util.Scanner;

public class KboatStringConvert
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a string: ");
        String str = in.nextLine();
        str = str.toUpperCase();
        int l = str.length();
        
        String res = "";
        
        for (int i = 0; i < l; i++) {
            char ch = str.charAt(i);
            if ("AEIOU".indexOf(ch) != -1) {
                res += (char)(ch + 1);
            }
            else if (Character.isLetter(ch)) {
                res += (char)(ch - 1);
            }
            else {
                res += ch;
            }
        }
        
        System.out.println("Output String:");
        System.out.println(res);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same. Example: Input : #IMAGINATION@2024 Output : #JLBFJMBSJPM@2024

Answered By

3 Likes


Related Questions