Program to Count Vowels From a String In Python Using Recursive Method
def isVowel(ch): return ch.upper() in ['A', 'E', 'I', 'O', 'U'] def countVovels(str, n): if (n == 1): return isVowel(str[n - 1]); return (countVovels(str, n - 1) + isVowel(str[n - 1])) str = "Python programming" print(countVovels(str, len(str)))
Code Explanation: Count Vowels From a String In Python Using Recursive Method
In this program, we have declared variable str with string value “Python programming”. And in the next line, we have called the function Count Vowels inside the print statement. Count Vowels accept two parameters which are string str and length of the string.
Inside the function, we have checked if n which is the length of the string is one then return the count, return call isVowel function with the last character of string if it is a vowel then function return 1 else 0.
Else function repeatedly call itself until the length of the string is 1, here we call the countvowels to function every time reducing the length of the string plus a count of the vowels. Let’s run this program. We will get the result 4 as the count of vowels.
If you want to learn python programming, you can refer to this Python Online Course with Certification. It’s easy to understand and meant for beginners who have no background in programming.