Write a Java test program that reads a String from the user and then prints the
ID: 3929775 • Letter: W
Question
Write a Java test program that reads a String from the user and then prints the following: Only the uppercase letters in the string. Every second letter in the string. The string with all the vowels replaced with an underscore. The number of vowels in the string. The positions of all the vowels in the string. This program should be contained in a single main method. Each of the print requirements above should be implemented in a separate loop. At least two of the loops must use while, the remainder may use for.Explanation / Answer
Hi, Please find my implementation.
Please let me know in case of any issue.
import java.util.Scanner;
public class StringOperation {
public static void main(String[] args) {
// Scanner Object to read user inout
Scanner sc =new Scanner(System.in);
// taking user input
System.out.println("Enter a string: ");
String input = sc.nextLine();
//1. printing Upper Case letter
for(int i=0; i<input.length(); i++){
char c = input.charAt(i); // current letter
if(Character.isLetter(c)){ // if current character is letter
if(Character.isUpperCase(c))
System.out.print(c);
}
}
System.out.println();
//2. printing every second letter in the string
int i=0;
int count = 0;
while(i < input.length()){
char c = input.charAt(i); // current letter
if(Character.isLetter(c)){ // if current character is letter
count++; // increase count
if(count == 2){ // if it is second letter
System.out.print(c);
count = 0; // set count to zero
}
}
i++;
}
System.out.println();
//3.replace vowel with upper case
String replace = "";
i = 0;
while(i < input.length()){
char c = input.charAt(i); // current letter
if(Character.isLetter(c)){ // if current character is letter
c= Character.toLowerCase(c); // converting to lower case
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
c = '_';
}
replace = replace + c;
i++;
}
System.out.println(replace);
System.out.println();
// 4. number of vowels in the string
i = 0;
int vowelCount = 0;
while(i < input.length()){
char c = input.charAt(i); // current letter
if(Character.isLetter(c)){ // if current character is letter
c= Character.toLowerCase(c); // converting to lower case
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
vowelCount++; // increasing vowel count
}
i++;
}
System.out.println("Number of vowel: "+vowelCount);
System.out.println();
//5. position of vowel
i = 0;
while(i < input.length()){
char c = input.charAt(i); // current letter
if(Character.isLetter(c)){ // if current character is letter
c= Character.toLowerCase(c); // converting to lower case
if(c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u')
System.out.print(i+" ");
}
i++;
}
System.out.println();
}
}
/*
Sample run:
Enter a string:
This is the test program by the way!!!.
T
hsshtspormyhwy
th_s _s th_ t_st pr_gr_m by th_ w_y!!!.
Number of vowel: 8
2 5 10 13 19 22 30 33
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.