Program to count vowels in a string (Iterative and Recursive)
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. 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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output:
2
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; } |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 |
chevron_right
filter_none
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 ?> |
chevron_right
filter_none
Output:
2
This article is contributed by Prakhar Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
Recommended Posts:
- Count consonants in a string (Iterative and recursive methods)
- Program to reverse a string (Iterative and Recursive)
- Python program to count number of vowels using sets in given string
- Program to count vowels, consonant, digits and special characters in string.
- First uppercase letter in a string (Iterative and Recursive)
- Function to copy string (Iterative and Recursive)
- Program for average of an array (Iterative and Recursive)
- Count the pairs of vowels in the given string
- Count the number of vowels occurring in all the substrings of given string
- Program to remove vowels from a String
- Program to duplicate Vowels in String
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- Check if a string can be converted to another string by replacing vowels and consonants
- Count substrings that contain all vowels | SET 2
- String slicing in Python to check if a string can become empty by recursive deletion




