(Find the number of uppercase letters in a string) Write a recursive function to return the number of uppercase letters in a string using the following function headers:
def countUppercase(s):
def countUppercaseHelper(s, high):
Write a test program that prompts the user to enter a string and displays the number of uppercase letters in the string.
Find the number of uppercase letters in a string Program Code:
#Recursive function 'countUppercase' accepts string parameter 's'
#to count the number of uppercase letters in a string.
def countUppercase(s):
return countUppercaseHelper(s, len(s) - 1)
#Recursive helper function 'countUppercaseHelper' accepts string 's'
# and hihest index 'high' to count the number of uppercase letters in a string.
def countUppercaseHelper(s, high):
# Base case: If the highest index is less than 0, we have checked all characters.
if high < 0:
return 0
# Recursive case: Check if the character at the current index is uppercase.
# If it is, increment the count by 1 and recursively call countUppercaseHelper
# with the previous index.
count = countUppercaseHelper(s, high - 1)
if s[high].isupper():
count += 1
return count
# Prompt the user for input
s = input("Enter a string: ")
# Count the number of uppercase letters in the string
uppercase_count = countUppercase(s)
# Display the result
print(f"The number of uppercase letters in the string is: {uppercase_count}")
Executed Output:
Enter a string: Hello Python WORLD
The number of uppercase letters in the string is: 7