Write a program using a function called Count () to accept a String form the user. Count and display the number of uppercase characters, the number of lowercase characters and the number of vowels as per the User's choice using Switch case. (i) to count the uppercase characters (ii) to count the lowercase characters (iii) to count the number of vowels.
Answers
Answer:
import java.util.Scanner;
public class CountingCharacters{
public static void main(String[] args){
count();
}
public static void count(){
Scanner sc = new Scanner(System.in);
int counter = 0;
System.out.println("Enter the string");
String st = sc.nextLine();
System.out.println("Enter: \n(i)to count Uppercase characters \n(ii)to count Lowercase characters \n(iii)to count vowels");
String choice = sc.next();
switch(choice){
case "i":
for(char ch: st.toCharArray()){
if(Character.isUpperCase(ch))
counter++;
}
System.out.println("No. of uppercase characters = "+counter);
break;
case "ii":
for(char ch: st.toCharArray()){
if(Character.isLowerCase(ch))
counter++;
}
System.out.println("No. of lowercase characters = "+counter);
break;
case "iii":
st = st.toLowerCase();
for(char ch: st.toCharArray()){
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
counter++;
}
System.out.println("No. of vowels = "+counter);
break;
default:
System.out.println("Wrong entry");
}
}
}
Explanation: