# 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
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

1. Iterative Method

## 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
```

Output:

```2
```

## Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.

2. Recursive Method

## 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
```

Output:

```2
```

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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

# GATE CS Corner    Company Wise Coding Practice

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
1.4 Average Difficulty : 1.4/5.0
Based on 12 vote(s)