{"id":20437,"date":"2021-09-12T12:02:02","date_gmt":"2021-09-12T06:32:02","guid":{"rendered":"https:\/\/python-programs.com\/?p=20437"},"modified":"2021-11-22T18:36:21","modified_gmt":"2021-11-22T13:06:21","slug":"python-program-to-count-number-of-uppercase-letters-in-a-string-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-number-of-uppercase-letters-in-a-string-using-recursion\/","title":{"rendered":"Python Program to Count Number of Uppercase Letters in a String using Recursion"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Count Number of Digits in a Number using Recursion<\/a><\/p>\n Given a string and the task is to count the number of uppercase letters present in a given string using recursion.<\/p>\n Recursion:<\/strong><\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to count the number of uppercase letters present in a given string using recursion in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n If you wanna write simple python programs as a part of your coding practice refer to numerous Simple Python Program Examples<\/a> existing and learn the approach used.<\/p>\n 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 …<\/p>\nGiven String = \"Hello This is Btechgeeks\"<\/pre>\n
The Number Of UpperCase characters Present in the above given String { Hello This is Btechgeeks } = 3<\/pre>\n
Given String = \"GOOD morning btechgeeks\"<\/pre>\n
The Number Of UpperCase characters Present in the above given String { GOOD morning btechgeeks } = 4<\/pre>\n
Program to Count Number of Uppercase Letters in a String using Recursion in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function to say cntUpprCase_chactrs which takes the given string and\r\n# a variable p (initially it is the length of the given string ) as the arguments to\r\n# the cntUpprCase_chactrs function which returns the count of uppercase characters in\r\n# a given string.\r\n\r\n\r\ndef cntUpprCase_chactrs(gvn_strng, p):\r\n # Make the cnt\u00a0a global declaration.\r\n global cnt\r\n # Check if the character present at the index p of the given string is greater than or\r\n # equal to 'A' and less than or equal to 'Z' using the if conditional statement.\r\n if (gvn_strng[p] >= 'A' and gvn_strng[p] <= 'Z'):\r\n # If the statement is true, then increment the value of cnt by 1 and store it in the\r\n # same variable.\r\n cnt += 1\r\n # Check if the value of p is greater than 0 using the if conditional statement.\r\n if (p > 0):\r\n # If the statement is true, pass the\u00a0given string and p-1 as the arguments to the\r\n # cntUpprCase_chactrs function.{Recursive logic}\r\n cntUpprCase_chactrs(gvn_strng, p - 1)\r\n # Return the value of cnt.\r\n return cnt\r\n\r\n\r\n# Give the string as static input and store it in a variable.\r\ngvn_strng = \"Hello This is Btechgeeks\"\r\n# Take a variable say cnt and initialize its value to 0.\r\ncnt = 0\r\n# Pass the given string and length of the given string-1 as the arguments to the\r\n# cntUpprCase_chactrs function and store it in a variable rslt_cnt.\r\nrslt_cnt = cntUpprCase_chactrs(gvn_strng, len(gvn_strng)-1)\r\n# Check if the value of rslt_cnt is equal to 0 using the if conditional statement.\r\nif(rslt_cnt == 0):\r\n # If the statement is true, then print \"There are no UpperCase characters in a given\r\n # string\".\r\n print(\"There are no UpperCase characters in a given string\")\r\nelse:\r\n # Else print the number of uppercase characters present in the above-given string.\r\n print(\r\n \"The Number Of UpperCase characters Present in the above given String {\", gvn_strng, \"} =\", rslt_cnt)\r\n<\/pre>\n
The Number Of UpperCase characters Present in the above given String { Hello This is Btechgeeks } = 3<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say cntUpprCase_chactrs which takes the given string and\r\n# a variable p (initially it is the length of the given string ) as the arguments to\r\n# the cntUpprCase_chactrs function which returns the count of uppercase characters in\r\n# a given string.\r\n\r\n\r\ndef cntUpprCase_chactrs(gvn_strng, p):\r\n # Make the cnt\u00a0a global declaration.\r\n global cnt\r\n # Check if the character present at the index p of the given string is greater than or\r\n # equal to 'A' and less than or equal to 'Z' using the if conditional statement.\r\n if (gvn_strng[p] >= 'A' and gvn_strng[p] <= 'Z'):\r\n # If the statement is true, then increment the value of cnt by 1 and store it in the\r\n # same variable.\r\n cnt += 1\r\n # Check if the value of p is greater than 0 using the if conditional statement.\r\n if (p > 0):\r\n # If the statement is true, pass the\u00a0given string and p-1 as the arguments to the\r\n # cntUpprCase_chactrs function {Recursive logic}\r\n cntUpprCase_chactrs(gvn_strng, p - 1)\r\n # Return the value of cnt.\r\n return cnt\r\n\r\n\r\n# Give the string as user input using the input() function and \r\n# store it in a variable.\r\ngvn_strng = input(\"Enter some Random String = \")\r\n# Take a variable say cnt and initialize its value to 0.\r\ncnt = 0\r\n# Pass the given string and length of the given string-1 as the arguments to the\r\n# cntUpprCase_chactrs function and store it in a variable rslt_cnt.\r\nrslt_cnt = cntUpprCase_chactrs(gvn_strng, len(gvn_strng)-1)\r\n# Check if the value of rslt_cnt is equal to 0 using the if conditional statement.\r\nif(rslt_cnt == 0):\r\n # If the statement is true, then print \"There are no UpperCase characters in a given\r\n # string\".\r\n print(\"There are no UpperCase characters in a given string\")\r\nelse:\r\n # Else print the number of uppercase characters present in the above-given string.\r\n print(\r\n \"The Number Of UpperCase characters Present in the above given String {\", gvn_strng, \"} =\", rslt_cnt)\r\n<\/pre>\n
Enter some Random String = GOOD morning btechgeeks\r\nThe Number Of UpperCase characters Present in the above given String { GOOD morning btechgeeks } = 4<\/pre>\n
\n