Skip to content
Related Articles

Related Articles

Python program to find maximum uppercase run

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 30 Jun, 2021

Giving a String, write a Python program to find the maximum run of uppercase characters.

Examples:

Input : test_str = ‘GeEKSForGEEksISBESt’ 
Output : 5 
Explanation : ISBES is best run of uppercase.

Input : test_str = ‘GeEKSForGEEKSISBESt’ 
Output : 10 
Explanation : GEEKSISBES is best run of uppercase. 

Method : Using isupper() + loop

In this, we update the maximum run when non-uppercase is encountered otherwise counter is incremented if the character is uppercase.

Python3




# Python3 code to demonstrate working of
# Maximum uppercase run
# Using isupper() + loop
 
# initializing string
test_str = 'GeEKSForGEEksIsBESt'
 
# printing original string
print("The original string is : " + str(test_str))
 
cnt = 0
res = 0
for idx in range(0, len(test_str)):
     
    # updating run count on uppercase
    if test_str[idx].isupper():
        cnt += 1
         
    # on lowercase, update the maxrun
    else :
      if res < cnt :
        res = cnt
        cnt = 0
      else :
        cnt = 0
if test_str[len(test_str) - 1].isupper():
    res = cnt
 
# printing result
print("Maximum Uppercase Run : " + str(res))

Output:

The original string is : GeEKSForGEEksISBESt
Maximum Uppercase Run : 5
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!