Techno Blender
Digitally Yours.

Python Program for Convert characters of a string to opposite case

0 43


View Discussion

Improve Article

Save Article

Like Article

View Discussion

Improve Article

Save Article

Like Article

Given a string, convert the characters of the string into opposite case,i.e. if a character is lower case then convert it into upper case and vice-versa. 

Examples: 

Input : geeksForgEeks
Output : GEEKSfORGeEKS

Input : hello every one
Output : HELLO EVERY ONE

ASCII values  of alphabets: A – Z = 65 to 90, a – z = 97 to 122 
Steps: 

  1. Take one string of any length and calculate its length.
  2. Scan string character by character and keep checking the index. 
    • If a character in an index is in lower case, then subtract 32 to convert it in upper case, else add 32 to convert it in lower case
  3. Print the final string.

Implementation:

Python3

 

def convertOpposite(str):

    ln = len(str)

 

    

    for i in range(ln):

        if str[i] >= 'a' and str[i] <= 'z':

 

            

            str[i] = chr(ord(str[i]) - 32)

 

        elif str[i] >= 'A' and str[i] <= 'Z':

 

            

            str[i] = chr(ord(str[i]) + 32)

 

if __name__ == "__main__":

    str = "GeEkSfOrGeEkS"

    str = list(str)

 

    

    convertOpposite(str)

 

    str = ''.join(str)

    print(str)

Time Complexity: O(n) 

Note: This program can alternatively be done using C++ inbuilt functions – Character.toLowerCase(char) and Character.toUpperCase(char). 

Approach 2: The problem can be solved using letter case toggling. Follow the below steps to solve the problem:

  • Traverse the given string S.
  • For each character Si, do Si =  Si ^ (1
  • Si ^ (1 toggles the 5th bit which means 97 will become 65 and 65 will become 97:
    • 65 ^ 32 = 97
    • 97 ^ 32 = 65 

  • Print the string after all operations

Below is the implementation of the above approach:

Python3

def isalpha(input):

        input_char = ord(input[0])

         

        

        if((input_char >= 65 and input_char <= 90) or (input_char >= 97 and input_char <= 122)):

            return True

        else:

            return False

 

def toggleChars(S):

   s = ""

 

   for it in range(len(S)):

 

      if(isalpha(S[it])):

         s += chr(ord(S[it])^(1<<5))

      else:

         s += S[it]

    

   return s

 

S = "GeKf@rGeek$"

print(f"String after toggle {toggleChars(S)}")

Output
String after toggle 
gEkF@RgEEK$

 

Time Complexity: O(n)

Python3

 

str = "GeEkSfOrGeEkS"

x=""

for i in str:

    if(i.isupper()):

        x+=i.lower()

    else:

        x+=i.upper()

print(x)

Please refer complete article on Convert characters of a string to opposite case for more details!


View Discussion

Improve Article

Save Article

Like Article

View Discussion

Improve Article

Save Article

Like Article

Given a string, convert the characters of the string into opposite case,i.e. if a character is lower case then convert it into upper case and vice-versa. 

Examples: 

Input : geeksForgEeks
Output : GEEKSfORGeEKS

Input : hello every one
Output : HELLO EVERY ONE

ASCII values  of alphabets: A – Z = 65 to 90, a – z = 97 to 122 
Steps: 

  1. Take one string of any length and calculate its length.
  2. Scan string character by character and keep checking the index. 
    • If a character in an index is in lower case, then subtract 32 to convert it in upper case, else add 32 to convert it in lower case
  3. Print the final string.

Implementation:

Python3

 

def convertOpposite(str):

    ln = len(str)

 

    

    for i in range(ln):

        if str[i] >= 'a' and str[i] <= 'z':

 

            

            str[i] = chr(ord(str[i]) - 32)

 

        elif str[i] >= 'A' and str[i] <= 'Z':

 

            

            str[i] = chr(ord(str[i]) + 32)

 

if __name__ == "__main__":

    str = "GeEkSfOrGeEkS"

    str = list(str)

 

    

    convertOpposite(str)

 

    str = ''.join(str)

    print(str)

Time Complexity: O(n) 

Note: This program can alternatively be done using C++ inbuilt functions – Character.toLowerCase(char) and Character.toUpperCase(char). 

Approach 2: The problem can be solved using letter case toggling. Follow the below steps to solve the problem:

  • Traverse the given string S.
  • For each character Si, do Si =  Si ^ (1
  • Si ^ (1 toggles the 5th bit which means 97 will become 65 and 65 will become 97:
    • 65 ^ 32 = 97
    • 97 ^ 32 = 65 

  • Print the string after all operations

Below is the implementation of the above approach:

Python3

def isalpha(input):

        input_char = ord(input[0])

         

        

        if((input_char >= 65 and input_char <= 90) or (input_char >= 97 and input_char <= 122)):

            return True

        else:

            return False

 

def toggleChars(S):

   s = ""

 

   for it in range(len(S)):

 

      if(isalpha(S[it])):

         s += chr(ord(S[it])^(1<<5))

      else:

         s += S[it]

    

   return s

 

S = "GeKf@rGeek$"

print(f"String after toggle {toggleChars(S)}")

Output
String after toggle 
gEkF@RgEEK$

 

Time Complexity: O(n)

Python3

 

str = "GeEkSfOrGeEkS"

x=""

for i in str:

    if(i.isupper()):

        x+=i.lower()

    else:

        x+=i.upper()

print(x)

Please refer complete article on Convert characters of a string to opposite case for more details!

FOLLOW US ON GOOGLE NEWS

Read original article here

Denial of responsibility! Techno Blender is an automatic aggregator of the all world’s media. In each content, the hyperlink to the primary source is specified. All trademarks belong to their rightful owners, all materials to their authors. If you are the owner of the content and do not want us to publish your materials, please contact us by email – [email protected]. The content will be deleted within 24 hours.
Leave a comment