Java Program to count vowels in a string – Here, we discuss the various methods to find vowels in a string in Java. The methods used in the same are Switch Case, Static Method and Function. We have also added compiler to each program and sample outputs citing specific examples.
Vowels are the words whose sounds are produced with an open vocal tract, giving them the distinct class of speech sound, apart from Consonants.
The English Alphabet series has 5 Vowels and 21 Consonants in the total combination of 26 Alphabets. Those are A, E, I, O & U.
There are several words in the English language that contains all the five vowels in contention. Those are as follows:
- Abstemious
- Facetious
- Arsenious
- Annelidous
- Uncomplimentary
- Education etc.
Thus, the methods used to find vowels in a given string in Java Programming are as follows:
Find Vowels In A String – Using Switch Case
1) Read the entered string using scanner class object sc.nextLine(), and store in the variable “s” which is string type.
2) The for loop iterates from j=0 to j< length of the string. The variable ch assigned with the charter which is at the jth position in the string.
3) If charter at ch matches with any one of these cases then i initialized to 1 and that charter will be printed. We don’t have break statement in any one of the cases, so switch executes all cases.
4) If no one cases match with the switch then i value remains 0.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import java.util.Scanner; class VowelsInaString { public static void main(String[ ] arg) { String s; char ch; int i=0; Scanner sc=new Scanner(System.in); System.out.print("Enter a string : "); s=sc.nextLine(); System.out.println("Vowels in a string are"); for(int j=0;j<s.length();j++) { ch=s.charAt(j); switch(ch) { case 'a' : case 'e' : case 'i' : case 'o' : case 'u' : case 'A' : case 'E' : case 'I' : case 'O' : case 'U' :i=1; System.out.println(ch); } } if(i==0) System.out.println("There are no vowels in a string"); } } |
Output:
1 2 3 4 5 6 7 |
Enter a string :java programming Vowels in a string are a a o a i |
1 2 3 4 5 6 7 |
Enter a string : Vowels in a string Vowels in a string are o e i a i |
Using Static Method
1) Read the entered string using scanner object sc.nextLint(), and store it in the string variable s.
2) Static method vowels(String str), will find vowels at the given string. This method calls at the main method as vowels(s), then vowels method will be executed.
3) In vowels(String str) method for loop iterates from j=0 to j< length of the string. , if ch matches with any one of the vowels then i=1 and ch will be printed. If no one ch of string array does not match with vowels then i=0 and prints “There are no vowels in an entered string”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import java.util.Scanner; class VowelsInaString { public static void main(String[ ] arg) { String s; Scanner sc=new Scanner(System.in); System.out.print("Enter a string : "); s=sc.nextLine(); System.out.println("Vowels in a string '"+s+"' are :"); vowels(s); } static void vowels(String str) { char ch; int i=0; for(int j=0;j<str.length();j++) { ch=str.charAt(j); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') { i=1; System.out.println(ch); } } if(i==0) System.out.println("There are no vowels in a entered string"); } } |
Output:
1 2 3 4 5 6 7 |
Enter a string : abcdefghijklmnopqrstuvwxyz Vowels in a string 'abcdefghijklmnopqrstuvwxyz' are : a e i o u |
Using Functions
1) In this program, we have vowels(String str) function to find the vowels in the entered string.
2) Read the entered String using scanner object sc.nextLine(), and store it in the string variable s.
3) The vowels(s) method calls at the main method by passing the entered string as an argument. This vowels method checks each charter at the given string with vowels. If any character matches with vowels then it prints that charter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Scanner; class VowelsInaString { void vowels(String str) { char ch; for(int j=0;j<str.length();j++) { ch=str.charAt(j); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') { System.out.println(ch); } } } public static void main(String[ ] arg) { CharInaString c=new CharInaString(); String s; Scanner sc=new Scanner(System.in); System.out.print("Enter a string : "); s=sc.nextLine(); System.out.println("Vowels in a string are :"); c.vowels(s); } } |
Output:
1 2 3 4 5 |
Enter a string : hello world Vowels in a string are : e o o |