Write a program which reads a String and displays the following output: Only the
ID: 3863822 • Letter: W
Question
Write a program which reads a String and displays the following output: Only the uppercase letters in the string. The string, with all vowels replaced by an underscore. The number of vowels in the string. The number of consonants in the string. The positions (indexes) of all vowels in the string. For the purposes of this program consider 'y' to be a consonant not a vowel. Input Validation: None required Requirements: Assume the string will have only uppercase and lowercase letters, and spaces. You are only allowed to use one for loop to solve this problem. No other loops allowed.Explanation / Answer
StringOperations.java
import java.util.Scanner;
import com.sun.org.apache.bcel.internal.generic.ISUB;
public class StringOperations {
public static void main(String[] args) {
//Declaring variables
String str;
int count_vowels=0,count_consonents=0;
String vowelsReplaced="";
//Scanner object is used to get the inputs entered by the user
Scanner sc=new Scanner(System.in);
//Getting the string entered by the user
System.out.print("Enter the String :");
str=sc.nextLine();
//Displaying the upper case letters by using the for loop
System.out.print("Uppercase Letters :");
for(int i=0;i<str.length();i++)
{
//Checking the letter is uppercase or not
if(Character.isUpperCase(str.charAt(i)))
{
System.out.print(str.charAt(i)+" ");
}
//Checking the letter is vowel or not
if(str.charAt(i)=='A'||str.charAt(i)=='E'||str.charAt(i)=='I'||str.charAt(i)=='O'||str.charAt(i)=='U'||
str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
{
vowelsReplaced+="_";
//Counting the no of vowels
count_vowels++;
}
else
{
vowelsReplaced+=str.charAt(i);
//Counting the no of consonants
count_consonents++;
}
}
//Diosplaying the output
System.out.println(" Vowels Replaced :"+vowelsReplaced);
System.out.println("No of Vowels :"+count_vowels);
System.out.println("No of Consonants :"+count_consonents);
System.out.print("Position of Vowels :");
//Displaying the position of vowels
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)=='A'||str.charAt(i)=='E'||str.charAt(i)=='I'||str.charAt(i)=='O'||str.charAt(i)=='U'||
str.charAt(i)=='a'||str.charAt(i)=='e'||str.charAt(i)=='i'||str.charAt(i)=='o'||str.charAt(i)=='u')
{
System.out.print(i+" ");
}
}
}
}
_____________________
Output:
Enter the String :And miles tO gO bEfoRe I sleeP
Uppercase Letters :A O O E R I P
Vowels Replaced :_nd m_l_s t_ g_ b_f_R_ _ sl__P
No of Vowels :11
No of Consonants :19
Position of Vowels :0 5 7 11 14 17 19 21 23 27 28
__Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.