Program to count vowels in a string (Iterative and Recursive)
Last Updated :
16 Feb, 2023
Given a string, count the total number of vowels (a, e, i, o, u) in it. There are two methods to count total number of vowels in a string.
- Iterative
- Recursive
Examples:
Input : abc de
Output : 2
Input : geeksforgeeks portal
Output : 7
1. Iterative Method:
Below is the implementation:
C++
// C++ program to count vowels in a string
#include<iostream>
using namespace std;
// Function to check the Vowel
bool isVowel(char ch)
{
ch = toupper(ch);
return (ch=='A' || ch=='E' || ch=='I' ||
ch=='O' || ch=='U');
}
// Returns count of vowels in str
int countVowels(string str)
{
int count = 0;
for (int i=0; i<str.length(); i++)
if (isVowel(str[i])) // Check for vowel
++count;
return count;
}
// Main Calling Function
int main()
{
//string object
string str = "abc de";
// Total numbers of Vowel
cout << countVowels(str) << endl;
return 0;
}
Java
// Java program to count vowels in a string
public class GFG {
// Function to check the Vowel
static boolean isVowel(char ch)
{
ch = Character.toUpperCase(ch);
return (ch=='A' || ch=='E' || ch=='I' ||
ch=='O' || ch=='U');
}
// Returns count of vowels in str
static int countVowels(String str)
{
int count = 0;
for (int i = 0; i < str.length(); i++)
if (isVowel(str.charAt(i))) // Check for vowel
++count;
return count;
}
// Driver code
public static void main(String args[])
{
//string object
String str = "abc de";
// Total numbers of Vowel
System.out.println(countVowels(str));
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to count vowels
# in a string
# Function to check the Vowel
def isVowel(ch):
return ch.upper() in ['A', 'E', 'I', 'O', 'U']
# Returns count of vowels in str
def countVowels(str):
count = 0
for i in range(len(str)):
# Check for vowel
if isVowel(str[i]):
count += 1
return count
# Driver Code
# string object
str = 'abc de'
# Total number of Vowels
print(countVowels(str))
# This code is contributed
# by SamyuktaSHegde
C#
// C# program to count vowels in a string
using System;
class GFG
{
// Function to check the Vowel
public static bool isVowel(char ch)
{
ch = char.ToUpper(ch);
return (ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U');
}
// Returns count of vowels in str
public static int countVowels(string str)
{
int count = 0;
for (int i = 0; i < str.Length; i++)
{
// Check for vowel
if (isVowel(str[i]))
{
++count;
}
}
return count;
}
// Driver code
public static void Main(string[] args)
{
//string object
string str = "abc de";
// Total numbers of Vowel
Console.WriteLine(countVowels(str));
}
}
// This code is contributed by Shrikant13
PHP
<?php
// PHP program to count vowels in a string
// Function to check the Vowel
function isVowel($ch)
{
$ch = strtoupper($ch);
return ($ch =='A' || $ch =='E' ||
$ch =='I' || $ch =='O' ||
$ch =='U');
}
// Returns count of vowels in str
function countVowels($str)
{
$count = 0;
for ($i = 0; $i < strlen($str); $i++)
if (isVowel($str[$i])) // Check for vowel
++$count;
return $count;
}
// Driver Code
//string object
$str = "abc de";
// Total numbers of Vowel
echo countVowels($str) . "\n";
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// JavaScript program to count vowels in a string
// Function to check the Vowel
function isVowel(ch) {
ch = ch.toUpperCase();
return ch == "A" || ch == "E" || ch == "I" || ch == "O" || ch == "U";
}
// Returns count of vowels in str
function countVowels(str)
{
var count = 0;
for (var i = 0; i < str.length; i++)
if (isVowel(str[i]))
// Check for vowel
++count;
return count;
}
// Main Calling Function
// string object
var str = "abc de";
// Total numbers of Vowel
document.write(countVowels(str));
document.write("<br>");
// This code is contributed by rdtank.
</script>
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(1)
2. Recursive Method:
Below is the implementation:
C++
// Recursive C++ program to count the total
// number of vowels using recursion
#include<iostream>
using namespace std;
// Function to check the Vowel
bool isVowel(char ch)
{
ch = toupper(ch);
return (ch=='A' || ch=='E' || ch=='I' ||
ch=='O' || ch=='U');
}
// to count total number of vowel from 0 to n
int countVovels(string str, int n)
{
if (n == 1)
return isVowel(str[n-1]);
return countVovels(str, n-1) + isVowel(str[n-1]);
}
// Main Calling Function
int main()
{
// string object
string str = "abc de";
// Total numbers of Vowel
cout << countVovels(str, str.length()) << endl;
return 0;
}
Java
// Recursive Java program to count the total
// number of vowels using recursion
public class GFG {
// Function to check the Vowel
static int isVowel(char ch)
{
ch = Character.toUpperCase(ch);
if(ch=='A' || ch=='E' || ch=='I' ||
ch=='O' || ch=='U')
return 1;
else return 0;
}
// to count total number of vowel from 0 to n
static int countVowels(String str, int n)
{
if (n == 1)
return isVowel(str.charAt(n - 1));
return countVowels(str, n-1) + isVowel(str.charAt(n - 1));
}
// Main Calling Function
public static void main(String args[])
{
//string object
String str = "abc de";
// Total numbers of Vowel
System.out.println(countVowels(str,str.length()));
}
}
// This code is contributed by Sumit Ghosh
Python 3
# Recursive Python 3 program to count the
# total number of vowels using recursion
# Function to check the Vowel
def isVowel(ch):
return ch.upper() in ['A', 'E', 'I', 'O', 'U']
# to count total number of
# vowel from 0 to n
def countVovels(str, n):
if (n == 1):
return isVowel(str[n - 1]);
return (countVovels(str, n - 1) +
isVowel(str[n - 1]));
# Driver Code
# string object
str = "abc de";
# Total numbers of Vowel
print(countVovels(str, len(str)))
# This code is contributed
# by Akanksha Rai
C#
using System;
// Recursive C# program to count the total
// number of vowels using recursion
public class GFG
{
// Function to check the Vowel
public static int isVowel(char ch)
{
ch = char.ToUpper(ch);
if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')
{
return 1;
}
else
{
return 0;
}
}
// to count total number of vowel from 0 to n
public static int countVowels(string str, int n)
{
if (n == 1)
{
return isVowel(str[n - 1]);
}
return countVowels(str, n - 1) + isVowel(str[n - 1]);
}
// Main Calling Function
public static void Main(string[] args)
{
//string object
string str = "abc de";
// Total numbers of Vowel
Console.WriteLine(countVowels(str,str.Length));
}
}
// This code is contributed by Shrikant13
PHP
<?php
// Recursive PHP program to count the total
// number of vowels using recursion
// Function to check the Vowel
function isVowel($ch)
{
$ch = strtoupper($ch);
return ($ch == 'A' || $ch == 'E' ||
$ch == 'I' || $ch == 'O' ||
$ch == 'U');
}
// to count total number of
// vowel from 0 to n
function countVovels($str, $n)
{
if ($n == 1)
return isVowel($str[$n - 1]);
return countVovels($str, $n - 1) +
isVowel($str[$n - 1]);
}
// Driver Code
// string object
$str = "abc de";
// Total numbers of Vowel
echo countVovels($str, strlen($str)) . "\n";
// This code is contributed
// by Akanksha Rai
?>
JavaScript
<script>
// Recursive JavaScript program to count the total
// number of vowels using recursion
// Function to check the Vowel
function isVowel(ch)
{
ch = ch.toUpperCase();
return (ch=='A' || ch=='E' || ch=='I' ||
ch=='O' || ch=='U');
}
// to count total number of vowel from 0 to n
function countVovels(str,n)
{
if (n == 1)
return isVowel(str[n-1]);
return countVovels(str, n-1) + isVowel(str[n-1]);
}
// Main Calling Function
// string object
let str = "abc de";
// Total numbers of Vowel
document.write(countVovels(str, str.length));
// This code is contributed by shinjanpatra
</script>
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), where n is the length of the string since the function is calling itself n times.
How Recursive Code Working.

Similar Reads
Count consonants in a string (Iterative and recursive methods) Given a string, count total number of consonants in it. A consonant is an English alphabet character that is not vowel (a, e, i, o and u). Examples of constants are b, c, d, f, and g. Examples : Input : abc de Output : 3 There are three consonants b, c and d. Input : geeksforgeeks portal Output : 12
7 min read
Program to count vowels, consonant, digits and special characters in string. Given a string and the task is to count vowels, consonant, digits and special character in string. Special character also contains the white space.Examples: Input : str = "geeks for geeks121" Output : Vowels: 5 Consonant: 8 Digit: 3 Special Character: 2 Input : str = " A1 B@ d adc" Output : Vowels:
6 min read
Remove consecutive vowels from string Given a string s of lowercase letters, we need to remove consecutive vowels from the string Note : Sentence should not contain two consecutive vowels ( a, e, i, o, u). Examples : Input: geeks for geeksOutput: geks for geksInput : your article is in queue Output : yor article is in quApproach: Iterat
15+ min read
Count the pairs of vowels in the given string Given a string str consisting of lowercase English alphabets, the task is to count the number of adjacent pairs of vowels.Examples: Input: str = "abaebio" Output: 2 (a, e) and (i, o) are the only valid pairs.Input: str = "aeoui" Output: 4 Approach: Starting from the first character of the string to
5 min read
Modify the string such that it contains all vowels at least once Given a string S containing only Uppercase letters, the task is to find the minimum number of replacement of characters needed to get a string with all vowels and if we cannot make the required string then print Impossible. Examples: Input: str = "ABCDEFGHI"Output: AOUDEFGHIExplanation: There are al
11 min read
Min. operations required so that no two vowels or consonants are adjacent Given a string S of size N. In one operation you can replace any character of the string with any other character, the task is to find the minimum number of operations required so that no two vowels or consonants are adjacent. Examples: Input: N = 4, S = 'wlrb'Output: 2Explanation: We can replace w
7 min read