KnowledgeBoat Logo

Computer Applications

Write a program to accept a word and convert it into lower case, if it is in upper case. Display the new word by replacing only the vowels with the letter following it.
Sample Input: computer
Sample Output: cpmpvtfr

Java

Java String Handling

ICSE 2011

170 Likes

Answer

import java.util.Scanner;

public class KboatVowelReplace
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a word: ");
        String str = in.nextLine();
        str = str.toLowerCase();
        String newStr = "";
        int len = str.length();

        for (int i = 0; i < len; i++) {
            char ch = str.charAt(i);

            if (str.charAt(i) == 'a' ||
            str.charAt(i) == 'e' ||
            str.charAt(i) == 'i' ||
            str.charAt(i) == 'o' ||
            str.charAt(i) == 'u') {

                char nextChar = (char)(ch + 1);
                newStr = newStr +  nextChar; 

            }
            else {
                newStr = newStr + ch;
            }
        }

        System.out.println(newStr);
    }
}

Variable Description Table

Program Explanation

Output

BlueJ output of Write a program to accept a word and convert it into lower case, if it is in upper case. Display the new word by replacing only the vowels with the letter following it. Sample Input: computer Sample Output: cpmpvtfr

Answered By

52 Likes


Related Questions