![]() |
VOOZH | about |
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:
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:
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)