KnowledgeBoat Logo
|
LoginJOIN NOW

Computer Applications

Write a program to input a word. Replace each consonant in the word with the previous letter. However, if the previous letter is a vowel, replace it with the next letter. Other characters should remain the same. Finally display the new word.

Sample input : CAT
Sample output : BAS
Sample input: BOOM
Sample output: COOL

Java

Java String Handling

13 Likes

Answer

import java.util.Scanner;

public class KboatString
{
    public static boolean isVowel(char ch) {
        char ip = Character.toUpperCase(ch);
        if (ip == 'A'
            || ip == 'E'
            || ip == 'I'
            || ip == 'O'
            || ip == 'U')
            return true;
        else
            return false;
    }
    
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter a word:");
        String str = in.nextLine();
        String newStr = "";
        int len = str.length();
        
        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);
            boolean f1 = isVowel(ch);
            if (f1) {
                newStr = newStr + ch;
            }
            else {
                char ch2 = (char)(ch - 1);
                boolean f2 = isVowel(ch2);
                if (f2) {
                    ch2 = (char)(ch + 1);
                }
                newStr = newStr + ch2;
            }
        }
        
        System.out.println("New Word");
        System.out.println(newStr);
    }
}

Output

BlueJ output of Write a program to input a word. Replace each consonant in the word with the previous letter. However, if the previous letter is a vowel, replace it with the next letter. Other characters should remain the same. Finally display the new word. Sample input : CAT Sample output : BAS Sample input: BOOM Sample output: COOLBlueJ output of Write a program to input a word. Replace each consonant in the word with the previous letter. However, if the previous letter is a vowel, replace it with the next letter. Other characters should remain the same. Finally display the new word. Sample input : CAT Sample output : BAS Sample input: BOOM Sample output: COOL

Answered By

4 Likes


Related Questions