Count vowels in a string Java
In this post, you will learn how to count the total number of vowels present in a string using the Java programming language. Such a type of logical coding question is generally asked in a programming interview or competitive examination. We should be aware that the solution to the most fundamental coding or programming questions decides how we perform in an interview. There are several ways to count the total number of vowels present in a given string. Here, we have mentioned most of them -
Count vowels in a string using for loop
Here is the Java program to count vowels using a forloop. In the given example, we have initialised a count variable with a 0 value. When the user entered the string as input, it passed through the for loop statement and compared each character in the sentence with the characters {'a', 'e', 'i', 'o', 'u' }. If a match occurs, increment the count.
// Java program for counting vowels
import java.util.Scanner;
public class CountingVowels {
public static void main(String args[]){
int count = 0;
System.out.println("Please enter a string :");
Scanner sc = new Scanner(System.in);
String string = sc.nextLine();
for (int i=0 ; i<string.length(); i++){
char ch = string.charAt(i);
if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'){
count ++;
}
}
System.out.println("Total number of vowels in the given sentence : "+count);
}
}
Output of the above code:
Please enter a string : elephant is a big animal
Total number of vowels in the given sentence : 9
Count vowels in a string using recursion
In the given example, we used the recursion function to count the total number of vowels in the specified string.
// Java program for counting vowels
import java.io.*;
public class CountingVowels {
static int countvowels(char chars)
{
if (chars == 'a' || chars == 'e' || chars == 'i'
|| chars == 'o' || chars == 'u') {
return 1;
}
else {
return 0;
}
}
static int countvowels(String str, int l)
{
if (l == 1) {
return countvowels(str.charAt(l - 1));
}
return countvowels(str, l - 1)
+ countvowels(str.charAt(l - 1));
}
public static void main(String[] args)
throws IOException
{
String str = "welcome to etutorialspoint";
str = str.toLowerCase();
System.out.println("Total number of vowels - ");
System.out.println(countvowels(str, str.length()));
}
}
Output of the above code:
Total number of vowels -
11
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number