Latest :

Java Program Count Vowels In A String | Programs

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.

Vowels

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.

Output:

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”.

Output:

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.

Output:

More Programs:

x

Check Also

Plus Star Pattern Java Program | Patterns

Java program to print plus star pattern program – We have written the below print/draw ...