VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-toggle-characters-string/

⇱ Program to toggle all characters in a string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to toggle all characters in a string

Last Updated : 1 Mar, 2023

Given a string, the task is to toggle all the characters of the string i.e to convert Upper case to Lower case and vice versa.

Examples: 

Input : gfg
Output : GFG

Input : aBc12#
Output : AbC12#

Input : tu@kmiNi
Output : TU@KMInI

Traverse the given string, if uppercase characters come, convert into lowercase and lowercase letter convert into uppercase.

Implementation:


Output
String after toggle 
gEkF@RgEEK$

Time complexity: O(n), where n is length of string
Auxiliary space: O(1)

We can use the Bitwise XOR operation to toggle all the characters in given string

As we know the ascii value of small and capital char case is differ by just 1 bit (5th bit). We can simply toggle that bit to toggle the char case of a string.

Implementation:


Output
String before toggle 
GeKf@rGeek$
String after toggle 
gEkF@RgEEK$

Time complexity: O(n), where n is the length of the string
Auxiliary space: O(1)

Comment
Article Tags:
Article Tags: