Home Java Programming Language / 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.

👁 145 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.