Computer Applications
Write a program to input a word. Replace each consonant in the word with the previous letter. However, if the previous letter is a vowel, replace it with the next letter. Other characters should remain the same. Finally display the new word.
Sample input : CAT
Sample output : BAS
Sample input: BOOM
Sample output: COOL
Java
Java String Handling
13 Likes
Answer
import java.util.Scanner;
public class KboatString
{
public static boolean isVowel(char ch) {
char ip = Character.toUpperCase(ch);
if (ip == 'A'
|| ip == 'E'
|| ip == 'I'
|| ip == 'O'
|| ip == 'U')
return true;
else
return false;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Enter a word:");
String str = in.nextLine();
String newStr = "";
int len = str.length();
for (int i = 0; i < len; i++) {
char ch = str.charAt(i);
boolean f1 = isVowel(ch);
if (f1) {
newStr = newStr + ch;
}
else {
char ch2 = (char)(ch - 1);
boolean f2 = isVowel(ch2);
if (f2) {
ch2 = (char)(ch + 1);
}
newStr = newStr + ch2;
}
}
System.out.println("New Word");
System.out.println(newStr);
}
}Output


Answered By
4 Likes
Related Questions
Write a Java program to enter any sentence and convert the sentence to uppercase. Print only those words of the sentence whose first and last letters are the same.
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each vowel by immediate next character and every consonant by the previous character. The other characters remain the same.
Example:
Input : #IMAGINATION@2024
Output : #JLBFJMBSJPM@2024Define a class to accept the gmail id and check for its validity.
A gmail id is valid only if it has:
→ @
→ .(dot)
→ gmail
→ com
Example:
icse2024@gmail.comis a valid gmail id