Java program to count number of vowels in a sentence and the program should continue till user enters a word “quit”.

Java program to count number of vowels in a sentence

Java program to count number of vowels in a sentence

This Java program to count number of vowels in a sentence is may chance to ask in Practical exam in which user will enters sentences from which we have to find vowels. And this process will continue till user enters a word “quit”. And finally display a total vowel are used in sentences.

Java program to count number of vowels in a sentence and the program should continue till user enters a word “quit”.

 

 Also Read: Swap two variables without using Third Variable in Java

 

Program:

import java.util.Scanner; 

class AEIOU
{ 
   public static void main(String[] args) 
   { 
          Scanner in = new Scanner(System.in);
          String str; 
          int A=0,E=0,I=0,O=0,U=0; 
          char ch; 
          System.out.println("Enter 'quit' to end:"); 
                
      while(true) 
      {
          System.out.println("Enter String: ");
          str = in.nextLine(); 
                 
          if (str.equals("quit")) 
          {
              break;
          } 
          else 
          {
              for (int i=0;i<str.length();i++) 
              { 
                   ch=str.charAt(i); 
                   if(ch=='a' || ch=='A') 
                   { 
                       A++; 
                   } 
                   else if(ch=='e' || ch=='E') 
                   { 
                       E++; 
                   }
                   else if(ch=='i' || ch=='I') 
                   { 
                       I++; 
                   } 
                   else if(ch=='o' || ch=='O') 
                   { 
                       O++; 
                   } 
                   else if(ch=='u' || ch=='U') 
                   { 
                       U++; 
                   } 
              } 
          } 
      } 
          System.out.println("A or a: "+A+"\nE or e: "+E+"\nI or i: "+I+"\nO or o: "+O+"\nU or u: "+U); 
          System.out.println("Total Vowels: "+(A+E+I+O+U));
   }
}

Download Java Program to count number of vowels in a sentence, click here.

As we all know vowels are five ‘A’,  ‘E’, ‘I’, ‘O’, ‘U’. So first we declare them and initialize them with zero. And we create a while loop and pass true as in condition because we have to run this program till user enters a word “quit”. we got a sentence from user through nextLine() method and store it in str. Then we put one condition that if entered keyword is quit then we break the loop and come out from it or else it will be continue.

If the condition will false then program controls goes into else part and execute it. In which we compare string character with all vowels and if it is matched then we increment that variables value by one. And in last we print occurrence of all the vowels and also total vowels are used in sentences.

Output:

Java program to count number of vowels in a sentence

So, I hope now you got a logic of ‘java program to count number of vowels in a sentence’.

Thank you!

Stay Connected with Place4Java and Facebook.

2 Trackbacks / Pingbacks

  1. Check strings are anagram or not in Java in a Simple Way !
  2. Final Modifier - Final Class, Final Variable, Final Method in Java

Leave a Reply

Your email address will not be published.


*