In: Computer Science
Write a java program that read a line of input as a sentence and display: Only the uppercase letters in the sentence. The sentence, with all lowercase vowels (i.e. “a”, “e”, “i”, “o”, and “u”) replaced by a strike symbol “*”.
Here is the Java code for both the problems.
Sample output is added at the end.
Code:
import java.util.Scanner;
public class StringDisplay {
/*method to check if a character is lowercase vowel*/
public static boolean isLowerVowel(char ch){
if(ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u'){ /*if chartacter is a vowel*/
return true;
}
else{
return false;
}
}
public static void main(String[] args){
Scanner scanner=new Scanner(System.in); /*creates a new Scanner object*/
System.out.println("Enter a sentence: ");
String inputString=scanner.nextLine(); /*takes input from user*/
StringBuilder vowelsReplaced=new StringBuilder(); /*creates a new StringBuilder object*/
System.out.println("Uppercase letters in sentence are: ");
for(int i=0;i<inputString.length();i++){ /*runs through all characters in string*/
if(Character.isUpperCase(inputString.charAt(i))){ /*checks if character is uppercase*/
System.out.print(inputString.charAt(i)+" "); /*prints character*/
}
}
for(int i=0;i<inputString.length();i++){ /*runs through all characters in string*/
if(isLowerVowel(inputString.charAt(i))){ /*checks if the character is lowercase vowel*/
vowelsReplaced.append('*'); /*append * to vowelsReplaced*/
}
else{
vowelsReplaced.append(inputString.charAt(i)); /*append the character to vowelsReplaced*/
}
}
System.out.println("\nSentence with lowercase vowels replaced by strike symbol is:\n"+vowelsReplaced.toString()); /*print the StringBuilder vowelsReplaced by converting it into string*/
}
}
Sample Output: