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
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
Related Questions
Write a program in Java to enter any sentence. Also ask the user to enter a word. Print the number of times the word entered is present in the sentence. If the word is not present in the sentence, then print an appropriate message.
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); }