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
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);
}
}
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.
Input Reading:
BufferedReader to read the input string from the user.Lowercase Conversion:
Processing Each Character:
a, e, i, o, u), it replaces it with the next character in the ASCII sequence.Output:
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.
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.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.