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.
Java Programming Language (Article) (Program)
29
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:
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); } }
Output:
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.
-
Input Reading:
- Uses
BufferedReader
to read the input string from the user.
- Uses
-
Lowercase Conversion:
- Converts the input string to lowercase to ensure consistency.
-
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.
-
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.
This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.