Home / Programs / Write a program to accept a word and convert it into lowercase if it is in uppercase, and display the new word by replacing only the vowels with the character following it.
Programming Example

Write a program to accept a word and convert it into lowercase if it is in uppercase, and display the new word by replacing only the vowels with the character following it.

👁 142 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Write a program to accept a word and convert it into lowercase if it is in uppercase, and display the new word by replacing only the vowels with the character following it.

Example:

Sample Input: computer

Sample Output: cpmpvtfr

Program Code

import java.io.*;

class Change {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s, s1 = "";
        int i, l;
        char ch;

        System.out.println("Enter a string:");
        s = br.readLine();
        s = s.toLowerCase(); // Convert the entire string to lowercase

        l = s.length();

        for (i = 0; i < l; i++) {
            ch = s.charAt(i);
            // Replace vowels with the next character
            if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') {
                ch = (char) (ch + 1);
            }
            s1 = s1 + ch; // Build the new string
        }

        System.out.println("New Word is " + s1);
    }
}

Explanation

Here is the table of variables for the program:

Variable Type Description
s String The input string provided by the user.
s1 String The resulting string after processing (vowels replaced).
i int Loop index for iterating through the string.
l int Length of the input string.
ch char Current character being processed from the string.

This table summarizes the variables used in the program and their purposes.

  1. Input Reading:

    • Uses BufferedReader to read the input string from the user.
  2. Lowercase Conversion:

    • Converts the input string to lowercase to ensure consistency.
  3. Processing Each Character:

    • Iterates through each character of the string.
    • If the character is a vowel (a, e, i, o, u), it replaces it with the next character in the ASCII sequence.
    • Appends the modified character to a new string.
  4. Output:

    • Prints the new word after processing all characters.

This program correctly handles both uppercase and lowercase inputs, converts them to lowercase, and processes vowels by replacing them with the subsequent character in the alphabet.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.