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 a program which extracts a part of the string "SUBMISSION" is as follows:
(a) "MISS"
(b) "MISSION"If
String str = "SUBMISSION";
write appropriate Java statements to get the above outputs.Which of the following returns a String?
- length()
- charAt(int)
- replace(char, char)
- indexOf(String)
Define 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.com
is a valid gmail idWrite the output of the following String methods:
String x= "Galaxy", y= "Games";
(a) System.out.println(x.charAt(0)==y.charAt(0));
(b) System.out.println(x.compareTo(y));