Given a string s consisting of English letters (both uppercase and lowercase), convert each character to its opposite case that is, change every lowercase letter to uppercase, and every uppercase letter to lowercase.
Examples:
Input : s = "geeksForgEeks"
Output : "GEEKSfORGeEKS"
Explanation : All lower case characters are changed into upper case and vice versa.Input : s = "SMALLcase"
Output : "smallCASE"
Explanation : All lower case characters are changed into upper case and vice versa.
Table of Content
Library-Based Case Toggling - O(n) Time and O(n) Space
The idea involves initializing an empty string. Iterate a for loop over the given string and check each character whether is lowercase or uppercase using isupper() and islower(). If lowercase converts the character to uppercase using upper() and append to empty string, similarly with uppercase.
#include <iostream>
#include <cctype>
using namespace std;
string toggleChar(string& str) {
string result = "";
// Iterate over the original string
for (int i = 0; i < str.length(); i++) {
char ch = str[i];
// Check the case of the character and
// toggle accordingly
if (isupper(ch))
result += tolower(ch);
else
result += toupper(ch);
}
return result;
}
int main() {
string str = "GeEkSfOrGeEkS";
string x = toggleChar(str);
cout << x << endl;
return 0;
}
public class GFG {
public static String toggleChar(String str) {
String result = "";
// Iterate over the original string
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
// Check the case of the character and
// toggle accordingly
if (Character.isUpperCase(ch))
result += Character.toLowerCase(ch);
else
result += Character.toUpperCase(ch);
}
return result;
}
public static void main(String[] args) {
String str = "GeEkSfOrGeEkS";
String x = toggleChar(str);
System.out.println(x);
}
}
def toggleChar(str):
result = ""
# Iterate over the original string
for i in range(len(str)):
ch = str[i]
# Check the case of the character and
# toggle accordingly
if ch.isupper():
result += ch.lower()
else:
result += ch.upper()
return result
if __name__ == "__main__":
str = "GeEkSfOrGeEkS"
x = toggleChar(str)
print(x)
using System;
class GFG {
static string toggleChar(string str) {
string result = "";
// Iterate over the original string
for (int i = 0; i < str.Length; i++) {
char ch = str[i];
// Check the case of the character and
// toggle accordingly
if (char.IsUpper(ch))
result += char.ToLower(ch);
else
result += char.ToUpper(ch);
}
return result;
}
static void Main() {
string str = "GeEkSfOrGeEkS";
string x = toggleChar(str);
Console.WriteLine(x);
}
}
function toggleChar(str) {
let result = "";
// Iterate over the original string
for (let i = 0; i < str.length; i++) {
const ch = str[i];
// Check the case of the character and
// toggle accordingly
if (ch === ch.toUpperCase())
result += ch.toLowerCase();
else
result += ch.toUpperCase();
}
return result;
}
// Driver Code
const str = "GeEkSfOrGeEkS";
const x = toggleChar(str);
console.log(x);
Output
gEeKsFoRgEeKs
Using ASCII values - O(n) Time and O(1) Space
Iterate through each character of the string and toggle its case using ASCII values: subtract 32 to convert lowercase to uppercase, and add 32 to convert uppercase to lowercase.
#include <iostream>
using namespace std;
string toggleChar(string &str) {
int ln = str.length();
// Conversion according to ASCII values
for (int i = 0; i < ln; i++) {
if (str[i] >= 'a' && str[i] <= 'z')
// Convert lowercase to uppercase
str[i] = str[i] - 32;
else if (str[i] >= 'A' && str[i] <= 'Z')
// Convert uppercase to lowercase
str[i] = str[i] + 32;
}
return str;
}
int main() {
string str = "GeEkSfOrGeEkS";
str = toggleChar(str);
cout << str;
return 0;
}
public class GFG {
public static String toggleChar(String str) {
int ln = str.length();
// Conversion according to ASCII values
char[] arr = str.toCharArray();
for (int i = 0; i < ln; i++) {
if (arr[i] >= 'a' && arr[i] <= 'z')
// Convert lowercase to uppercase
arr[i] = (char)(arr[i] - 32);
else if (arr[i] >= 'A' && arr[i] <= 'Z')
// Convert uppercase to lowercase
arr[i] = (char)(arr[i] + 32);
}
return new String(arr);
}
public static void main(String[] args) {
String str = "GeEkSfOrGeEkS";
str = toggleChar(str);
System.out.println(str);
}
}
def toggleChar(str):
ln = len(str)
# Conversion according to ASCII values
for i in range(ln):
if str[i] >= 'a' and str[i] <= 'z':
# Convert lowercase to uppercase
str[i] = chr(ord(str[i]) - 32)
elif str[i] >= 'A' and str[i] <= 'Z':
# Convert lowercase to uppercase
str[i] = chr(ord(str[i]) + 32)
if __name__ == "__main__":
str = "GeEkSfOrGeEkS"
str = list(str)
toggleChar(str)
str = ''.join(str)
print(str)
using System;
using System.Text;
class GFG {
static string toggleChar(string str){
int ln = str.Length;
StringBuilder result = new StringBuilder(str);
// Conversion according to ASCII values
for (int i = 0; i < ln; i++){
if (result[i] >= 'a' && result[i] <= 'z')
// Convert lowercase to uppercase
result[i] = (char)(result[i] - 32);
else if (result[i] >= 'A' && result[i] <= 'Z')
// Convert uppercase to lowercase
result[i] = (char)(result[i] + 32);
}
return result.ToString();
}
public static void Main() {
string str = "GeEkSfOrGeEkS";
string toggled = toggleChar(str);
Console.WriteLine(toggled);
}
}
function toggleChar(str) {
const chars = str.split('');
const ln = chars.length;
// Conversion according to ASCII values
for (let i = 0; i < ln; i++) {
const code = chars[i].charCodeAt(0);
if (code >= 97 && code <= 122) {
// Convert lowercase to uppercase (subtract 32)
chars[i] = String.fromCharCode(code - 32);
} else if (code >= 65 && code <= 90) {
// Convert uppercase to lowercase (add 32)
chars[i] = String.fromCharCode(code + 32);
}
}
return chars.join('');
}
// Driver Code
const str = "GeEkSfOrGeEkS";
console.log(toggleChar(str));
Output
gEeKsFoRgEeKs
Toggle Case Using XOR on 5th Bit - O(n) Time and O(1) Space
The core logic behind letter case toggling is based on the ASCII representation of characters. In ASCII, the difference between any lowercase and its corresponding uppercase letter is always 32 - for example, 'a' - 'A' = 32, 'b' - 'B' = 32, and so on. This means that toggling the 5th bit (i.e., 1 << 5 = 32) of an alphabetic character using XOR (ch ^= 1 << 5) effectively switches its case - converting lowercase to uppercase and vice versa in constant time.
#include <iostream>
using namespace std;
string toggleChar(string S) {
for (auto &ch : S) {
// Toggle the 5th bit to flip case
ch ^= (1 << 5);
}
return S;
}
int main() {
string S = "GeEkSfOrGeEkS";
cout << toggleChar(S);
return 0;
}
class GFG {
static String toggleChar(String input) {
// Convert string to char array
char[] chars = input.toCharArray();
// Iterate over the string in order to flip the 5th bit.
for (int i = 0; i < chars.length; i++) {
if (Character.isAlphabetic(chars[i])) {
// Toggle the 5th bit to flip case
chars[i] ^= (1 << 5);
}
}
return new String(chars);
}
public static void main(String[] args) {
String S = "GeEkSfOrGeEkS";
String toggled = toggleChar(S);
System.out.println(toggled);
}
}
def toggleChar(s):
ans = ""
# Iterate over the string in order to flip the 5th bit.
for ch in s:
# Toggle the 5th bit to flip case
ans += chr(ord(ch) ^ (1 << 5))
return ans
if __name__ == "__main__":
s = "GeEkSfOrGeEkS"
result = toggleChar(s)
print(result)
using System;
class GFG {
static string toggleChar(string input){
// Iterate over the string in order to flip the 5th bit.
char[] chars = input.ToCharArray();
for (int i = 0; i < chars.Length; i++) {
if (char.IsLetter(chars[i])) {
// Toggle the 5th bit to flip case
chars[i] = (char)(chars[i] ^ (1 << 5));
}
}
return new string(chars);
}
public static void Main(string[] args){
string S = "GeEkSfOrGeEkS";
string toggled = toggleChar(S);
Console.WriteLine(toggled);
}
}
function toggleChar(str) {
const chars = str.split('');
for (let i = 0; i < chars.length; i++) {
const code = chars[i].charCodeAt(0);
// Iterate over the string in order to flip the 5th bit.
if ((code >= 65 && code <= 90) ||
(code >= 97 && code <= 122)) {
// Toggle the 5th bit to flip case
chars[i] = String.fromCharCode(code ^ (1 << 5));
}
}
return chars.join('');
}
// Driver code
const s = "GeEkSfOrGeEkS";
console.log(toggleChar(s));
Output
gEeKsFoRgEeKs