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
The output of the statement "CONCENTRATION".indexOf('T') is:
- 9
- 7
- 6
- (-1)
The output of the statement "talent".compareTo("genius") is:
- 11
- ā11
- 0
- 13
The following code to compare two strings is compiled, the following syntax error was displayed ā incompatible types ā int cannot be converted to boolean.
Identify the statement which has the error and write the correct statement. Give the output of the program segment.
void calculate() { String a = "KING", b = "KINGDOM"; boolean x = a.compareTo(b); System.out.println(x); }