Program to count vowels in a string (Iterative and Recursive)

02 Jun, 2022
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. 1. Iterative 2. RecursiveExamples:  
Input : abc de
Output : 2

Input : geeksforgeeks portal
Output : 7
 
1. Iterative Method Below is the implementation:  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to count vowels in a string#include<iostream>using namespace std; // Function to check the Vowelbool isVowel(char ch){    ch = toupper(ch);    return (ch=='A' || ch=='E' || ch=='I' ||                       ch=='O' || ch=='U');} // Returns count of vowels in strint 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 Functionint main(){    //string object    string str = "abc de";     // Total numbers of Vowel    cout << countVowels(str) << endl;    return 0;}
// Java program to count vowels in a stringpublic 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 program to count vowels# in a string # Function to check the Voweldef isVowel(ch):    return ch.upper() in ['A', 'E', 'I', 'O', 'U'] # Returns count of vowels in strdef countVowels(str):    count = 0    for i in range(len(str)):         # Check for vowel        if isVowel(str[i]):            count += 1    return count # Driver Code # string objectstr = 'abc de' # Total number of Vowelsprint(countVowels(str)) # This code is contributed# by SamyuktaSHegde
// C# program to count vowels in a stringusing 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 program to count vowels in a string // Function to check the Vowelfunction isVowel($ch){    $ch = strtoupper($ch);    return ($ch =='A' || $ch =='E' ||            $ch =='I' || $ch =='O' ||            $ch =='U');} // Returns count of vowels in strfunction 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 Vowelecho countVowels($str) . "\n"; // This code is contributed// by Akanksha Rai?>
<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>
Output:  
2
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(1) 
2. Recursive Method Below is the implementation:  
C++
Java
Python 3
C#
PHP
Javascript
// Recursive C++ program to count the total// number of vowels using recursion#include<iostream>using namespace std; // Function to check the Vowelbool 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 nint countVovels(string str, int n){    if (n == 1)        return isVowel(str[n-1]);     return countVovels(str, n-1) + isVowel(str[n-1]);} // Main Calling Functionint main(){    // string object    string str = "abc de";     // Total numbers of Vowel    cout << countVovels(str, str.length()) << endl;    return 0;}
// Recursive Java program to count the total// number of vowels using recursionpublic 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
# Recursive Python 3 program to count the# total number of vowels using recursion # Function to check the Voweldef isVowel(ch):    return ch.upper() in ['A', 'E', 'I', 'O', 'U'] # to count total number of# vowel from 0 to ndef countVovels(str, n):    if (n == 1):        return isVowel(str[n - 1]);     return (countVovels(str, n - 1) +                isVowel(str[n - 1])); # Driver Code # string objectstr = "abc de"; # Total numbers of Vowelprint(countVovels(str, len(str))) # This code is contributed# by Akanksha Rai
using System; // Recursive C# program to count the total// number of vowels using recursionpublic 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// Recursive PHP program to count the total// number of vowels using recursion // Function to check the Vowelfunction 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 nfunction 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 Vowelecho countVovels($str, strlen($str)) . "\n"; // This code is contributed// by Akanksha Rai?>
<script> // Recursive JavaScript program to count the total// number of vowels using recursion // Function to check the Vowelfunction 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 nfunction countVovels(str,n){    if (n == 1)        return isVowel(str[n-1]);     return countVovels(str, n-1) + isVowel(str[n-1]);} // Main Calling Function // string objectlet str = "abc de"; // Total numbers of Voweldocument.write(countVovels(str, str.length)); // This code is contributed by shinjanpatra </script>
Output:  
2
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. 
This article is contributed by Prakhar Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
shrikanth13
SamyuktaSHegde
Akanksha_Rai
rdtank
rishavnitro
shinjanpatra
rohitmishra051000
CBSE - Class 11
Java-String-Programs
school-programming
vowel-consonant
Java
School Programming
Strings
Strings
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to iterate any Map in Java
Interfaces in Java
HashMap in Java with Examples
Stream In Java
ArrayList in Java
Python Dictionary
Arrays in C/C++
Introduction To PYTHON
Interfaces in Java
Inheritance in C++