In the previous article, we have discussed Python Program to Count Number of Digits in a Number using Recursion
Given a string and the task is to count the number of uppercase letters present in a given string using recursion.
Recursion:
Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.
Examples:
Example1:
Input:
Given String = "Hello This is Btechgeeks"
Output:
The Number Of UpperCase characters Present in the above given String { Hello This is Btechgeeks } = 3Example2:
Input:
Given String = "GOOD morning btechgeeks"
Output:
The Number Of UpperCase characters Present in the above given String { GOOD morning btechgeeks } = 4Program to Count Number of Uppercase Letters in a String using Recursion in Python
Below are the ways to count the number of uppercase letters present in a given string using recursion in python:
Method #1: Using Recursion (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Take a variable say cnt and initialize its value to 0.
- Pass the given string and length of the given string-1 as the arguments to the cntUpprCase_chactrs function and store it in a variable rslt_cnt.
- Create a recursive function to say cntUpprCase_chactrs which takes the given string and a variable p (initially it is the length of the given string ) and returns the count of uppercase characters in a given string.
- Make the cnt a global declaration.
- Check if the character present at the index p of the given string is greater than or equal to ‘A’ and less than or equal to ‘Z’ using the if conditional statement.
- If the statement is true, then increment the value of cnt by 1 and store it in the same variable.
- Check if the value of p is greater than 0 using the if conditional statement.
- If the statement is true, pass the given string and p-1 as the arguments to the cntUpprCase_chactrs function.{Recursive logic}
- Return the value of cnt.
- Check if the value of rslt_cnt is equal to 0 using the if conditional statement.
- If the statement is true, then print “There are no UpperCase characters in a given string”.
- Else print the number of uppercase characters present in the above-given string.
- The Exit of the Program.
Below is the implementation:
# Create a recursive function to say cntUpprCase_chactrs which takes the given string and
# a variable p (initially it is the length of the given string ) as the arguments to
# the cntUpprCase_chactrs function which returns the count of uppercase characters in
# a given string.
def cntUpprCase_chactrs(gvn_strng, p):
# Make the cnt a global declaration.
global cnt
# Check if the character present at the index p of the given string is greater than or
# equal to 'A' and less than or equal to 'Z' using the if conditional statement.
if (gvn_strng[p] >= 'A' and gvn_strng[p] <= 'Z'):
# If the statement is true, then increment the value of cnt by 1 and store it in the
# same variable.
cnt += 1
# Check if the value of p is greater than 0 using the if conditional statement.
if (p > 0):
# If the statement is true, pass the given string and p-1 as the arguments to the
# cntUpprCase_chactrs function.{Recursive logic}
cntUpprCase_chactrs(gvn_strng, p - 1)
# Return the value of cnt.
return cnt
# Give the string as static input and store it in a variable.
gvn_strng = "Hello This is Btechgeeks"
# Take a variable say cnt and initialize its value to 0.
cnt = 0
# Pass the given string and length of the given string-1 as the arguments to the
# cntUpprCase_chactrs function and store it in a variable rslt_cnt.
rslt_cnt = cntUpprCase_chactrs(gvn_strng, len(gvn_strng)-1)
# Check if the value of rslt_cnt is equal to 0 using the if conditional statement.
if(rslt_cnt == 0):
# If the statement is true, then print "There are no UpperCase characters in a given
# string".
print("There are no UpperCase characters in a given string")
else:
# Else print the number of uppercase characters present in the above-given string.
print(
"The Number Of UpperCase characters Present in the above given String {", gvn_strng, "} =", rslt_cnt)
Output:
The Number Of UpperCase characters Present in the above given String { Hello This is Btechgeeks } = 3Method #2: Using Recursion (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Take a variable say cnt and initialize its value to 0.
- Pass the given string and length of the given string-1 as the arguments to the cntUpprCase_chactrs function and store it in a variable rslt_cnt.
- Create a recursive function to say cntUpprCase_chactrs which takes the given string and a variable p (initially it is the length of the given string ) as the arguments to the cntUpprCase_chactrs function which returns the count of uppercase characters in a given string.
- Make the cnt a global declaration.
- Check if the character present at the index p of the given string is greater than or equal to ‘A’ and less than or equal to ‘Z’ using the if conditional statement.
- If the statement is true, then increment the value of cnt by 1 and store it in the same variable.
- Check if the value of p is greater than 0 using the if conditional statement.
- If the statement is true, pass the given string and p-1 as the arguments to the cntUpprCase_chactrs function.{Recursive logic}
- Return the value of cnt.
- Check if the value of rslt_cnt is equal to 0 using the if conditional statement.
- If the statement is true, then print “There are no UpperCase characters in a given string”.
- Else print the number of uppercase characters present in the above-given string.
- The Exit of the Program.
Below is the implementation:
# Create a recursive function to say cntUpprCase_chactrs which takes the given string and
# a variable p (initially it is the length of the given string ) as the arguments to
# the cntUpprCase_chactrs function which returns the count of uppercase characters in
# a given string.
def cntUpprCase_chactrs(gvn_strng, p):
# Make the cnt a global declaration.
global cnt
# Check if the character present at the index p of the given string is greater than or
# equal to 'A' and less than or equal to 'Z' using the if conditional statement.
if (gvn_strng[p] >= 'A' and gvn_strng[p] <= 'Z'):
# If the statement is true, then increment the value of cnt by 1 and store it in the
# same variable.
cnt += 1
# Check if the value of p is greater than 0 using the if conditional statement.
if (p > 0):
# If the statement is true, pass the given string and p-1 as the arguments to the
# cntUpprCase_chactrs function {Recursive logic}
cntUpprCase_chactrs(gvn_strng, p - 1)
# Return the value of cnt.
return cnt
# Give the string as user input using the input() function and
# store it in a variable.
gvn_strng = input("Enter some Random String = ")
# Take a variable say cnt and initialize its value to 0.
cnt = 0
# Pass the given string and length of the given string-1 as the arguments to the
# cntUpprCase_chactrs function and store it in a variable rslt_cnt.
rslt_cnt = cntUpprCase_chactrs(gvn_strng, len(gvn_strng)-1)
# Check if the value of rslt_cnt is equal to 0 using the if conditional statement.
if(rslt_cnt == 0):
# If the statement is true, then print "There are no UpperCase characters in a given
# string".
print("There are no UpperCase characters in a given string")
else:
# Else print the number of uppercase characters present in the above-given string.
print(
"The Number Of UpperCase characters Present in the above given String {", gvn_strng, "} =", rslt_cnt)
Output:
Enter some Random String = GOOD morning btechgeeks
The Number Of UpperCase characters Present in the above given String { GOOD morning btechgeeks } = 4If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples existing and learn the approach used.
