Open In App

Python | Toggle characters in words having same case

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string, toggle the characters of those words who have all their characters in the same case. 

Examples:

Input : Geeks for Geeks 
Output : Geeks FOR Geeks 
Explanation: The string contains three words “Geeks”, “for”, “Geeks” out of which the word “for” contains all its characters as lower cases, therefore, toggle the case of the word. Print the remaining characters as it is. 

input : HELLO world 
Output : hello WORLD

Simple Approach: Consider each word in the string separately. Set two flags, one to check if all the cases of the characters are the same and another to check if the characters are upper case or lower case.

if all the characters are in the same case 
    if all the characters are lower case 
       then convert into upper case
    else convert into lower case
else print the same word

Below is the implementation: 

Python3




# Python program to toggle character in
# a string only with same case
 
# Function to toggle case
def toggle(s):
     
    word_list = s.split()
     
    # traverse through each word of the string
    for word in word_list:
         
        # initially assume all the characters in word
        # are in the same case and set the flag to 1
        flag = 1
         
        # check the case of the first
        # character in the word
        if word[0].islower():
             
            # if the case is lower set the r value to 1
            r = 1
             
            # traverse through the remaining
            # characters in the word
            for j in word:
                 
                # if any of the characters are in upper case
                if j.isupper():
                     
                    # then set the flag to 0
                    flag = 0
                    break
                 
        else:
            # if the case is upper
            # set the r value to 1
            r = 0
             
            # traverse through the remaining
            # characters in the word
            for j in word:
                 
                # if any of the characters are in lower case
                if j.islower():
                     
                    # then set the flag to 0
                    flag = 0
                    break
         
        # if the flag is 0 then print the word as it is   
        if flag == 0: print(word, end =" ")
     
        else:
            # if the word is in lower case
            # then print the word in upper case
            if r == 1: print(word.upper(), end =" ")
             
            # if the word is in upper case
            # then print the word in lower case
            else: print(word.lower(), end =" ")
             
# driver code
s = 'Geeks for Geeks'
toggle(s)


Output

Geeks FOR Geeks 

Complexity Analysis:

  • Time Complexity: O(n)
  • Auxiliary Space: O(n)

Using Library functions : We can also use direct library functions to check if individual words are lower or upper. 

Python3




# Python program to toggle character in
# a string only with same case
 
# Function to toggle case
def toggle(s):
     
    word_list = s.split()
     
    # traverse through each word of the string
    for word in word_list:
         
        if word.islower() :
            print(word.upper(), end =" ")
        elif word.isupper() :
            print(word.lower(), end =" ")
        else :
            print(word, end =" ")
             
# driver code
s = 'Geeks for Geeks'
toggle(s)


Output

Geeks FOR Geeks 

Complexity Analysis:

  • Time Complexity: O(n)
  • Auxiliary Space: O(n)

Using all() and list comprehension:

Here is a new approach Using all() and list comprehension.

Python3




def toggle_words(s):
    def toggle_word(word):
        if all(c.isupper() for c in word):
            return word.lower()
        elif all(c.islower() for c in word):
            return word.upper()
        else:
            return word
    return ' '.join(toggle_word(word) for word in s.split())
 
# Test the function
s = 'Geeks for Geeks'
print(toggle_words(s))  # Output: 'Geeks FOR Geeks'
 
s = 'HELLO world'
print(toggle_words(s))  # Output: 'hello WORLD'
 
#This code is contributed by Edula Vinay Kumar Reddy


Output

Geeks FOR Geeks
hello WORLD

This approach defines a generator function toggle_word which yields the toggled case of a single word. The generator function is used in a list comprehension to toggle the case of each word in the input string, and the resulting list is joined with spaces to form the final output string.

The time complexity of this approach is O(n), where n is the length of the input string. The space complexity is also O(n), as the input string is split into a list of words, and the output string is constructed by joining the toggled words with spaces. 



Last Updated : 09 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads