Given a string, convert the characters of the string into the opposite case,i.e. if a character is the 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:
- Take one string of any length and calculate its length.
- 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 into upper case, else add 32 to convert it in lowercase
- Print the final string.
Implementation:
C++
#include <iostream>
using namespace std;
void convertOpposite(string& str)
{
int ln = str.length();
for (int i = 0; i < ln; i++) {
if (str[i] >= 'a' && str[i] <= 'z')
str[i] = str[i] - 32;
else if (str[i] >= 'A' && str[i] <= 'Z')
str[i] = str[i] + 32;
}
}
int main()
{
string str = "GeEkSfOrGeEkS";
convertOpposite(str);
cout << str;
return 0;
}
|
Java
class Test {
static void convertOpposite(StringBuffer str)
{
int ln = str.length();
for (int i = 0; i < ln; i++) {
Character c = str.charAt(i);
if (Character.isLowerCase(c))
str.replace(i, i + 1,
Character.toUpperCase(c) + "");
else
str.replace(i, i + 1,
Character.toLowerCase(c) + "");
}
}
public static void main(String[] args)
{
StringBuffer str
= new StringBuffer("GeEkSfOrGeEkS");
convertOpposite(str);
System.out.println(str);
}
}
|
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)
|
C#
using System;
using System.Text;
class GFG{
static void convertOpposite(StringBuilder str)
{
int ln = str.Length;
for (int i=0; i<ln; i++)
{
if (str[i]>='a' && str[i]<='z')
str[i] = (char)(str[i] - 32);
else if(str[i]>='A' && str[i]<='Z')
str[i] = (char)(str[i] + 32);
}
}
public static void Main()
{
StringBuilder str = new StringBuilder("GeEkSfOrGeEkS");
convertOpposite(str);
Console.WriteLine(str);
}
}
|
Javascript
<script>
function convertOpposite(str)
{
var ln = str.length;
for (var i = 0; i < ln; i++)
{
if (str[i] >= 'a' && str[i] <= 'z')
document.write(
String.fromCharCode(str.charCodeAt(i) - 32)
);
else if (str[i] >= 'A' && str[i] <= 'Z')
document.write(
String.fromCharCode(str.charCodeAt(i) + 32)
);
}
}
var str = "GeEkSfOrGeEkS";
convertOpposite(str);
</script>
|
PHP
<?php
function convertOpposite($str)
{
$ln = strlen($str);
for ($i = 0; $i < $ln; $i++)
{
if ($str[$i] >= 'a' && $str[$i] <= 'z'){
$str[$i] = strtoupper($str[$i]);
}
else if ($str[$i] >= 'A' && $str[$i] <= 'Z'){
$str[$i] = strtolower($str[$i]);
}
}
return $str;
}
$str = "GeEkSfOrGeEkS";
echo(convertOpposite($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 << 5).
- Si ^ (1 << 5) toggles the 5th bit which means 97 will become 65 and 65 will become 97:
- Print the string after all operations
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void toggleChars(string& S)
{
for (auto& it : S) {
if (isalpha(it)) {
it ^= (1 << 5);
}
}
}
int main()
{
string S = "GeKf@rGeek$";
toggleChars(S);
cout << "String after toggle " << endl;
cout << S << endl;
return 0;
}
|
Java
import java.util.*;
class GFG {
static char[] S = "GeKf@rGeek$".toCharArray();
static void toggleChars()
{
for (int i = 0; i < S.length; i++) {
if (Character.isAlphabetic(S[i])) {
S[i] ^= (1 << 5);
}
}
}
public static void main(String[] args)
{
toggleChars();
System.out.print("String after toggle "
+ "\n");
System.out.print(String.valueOf(S));
}
}
|
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)}")
|
C#
using System;
class GFG {
static char[] S = "GeKf@rGeek$".ToCharArray();
static void toggleChars()
{
for (int i = 0; i < S.Length; i++) {
if (char.IsLetter(S[i])) {
S[i] = (char)((int)(S[i]) ^ (1 << 5));
}
}
}
public static void Main(String[] args)
{
toggleChars();
Console.Write("String after toggle "
+ "\n");
Console.Write(String.Join("", S));
}
}
|
Javascript
<script>
function isalpha(input)
{
var input_char = input.charCodeAt(0);
if (
(input_char >= 65 && input_char <= 90) ||
(input_char >= 97 && input_char <= 122))
{
return true;
}
else
{
return false;
}
}
function toggleChars(S)
{
var s = "";
for(var it = 0; it < S.length; it++)
{
if(isalpha(S.charAt(it)))
{
s += String.fromCharCode(S.charCodeAt(it)^(1<<5))
}
else{
s += S.charAt(it);
}
}
return s;
}
var S = "GeKf@rGeek$";
document.write( "String after toggle " +toggleChars(S));
</script>
|
OutputString after toggle
gEkF@RgEEK$
Time Complexity: O(n)
This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Approach : Using isupper() and islower(),upper() and lower().
Initialize 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.
Java
import java.io.*;
class GFG {
public static void main(String[] args)
{
String str = "GeEkSfOrGeEkS";
String x = "";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (Character.isUpperCase(ch))
x += Character.toLowerCase(ch);
else
x += Character.toUpperCase(ch);
}
System.out.println(x);
}
}
|
Python3
str = "GeEkSfOrGeEkS"
x=""
for i in str:
if(i.isupper()):
x+=i.lower()
else:
x+=i.upper()
print(x)
|
C#
using System;
public class GFG {
static public void Main()
{
string str = "GeEkSfOrGeEkS";
string x = "";
for (int i = 0; i < str.Length; i++) {
char ch = str[i];
if (Char.IsUpper(ch)) {
x += Char.ToLower(ch);
}
else {
x += Char.ToUpper(ch);
}
}
Console.WriteLine(x);
}
}
|
Javascript
let str = "GeEkSfOrGeEkS";
let x = "";
for (let i = 0; i < str.length; i++) {
let ch = str[i];
if(str.charAt(i) === str.charAt(i).toUpperCase())
x += str.charAt(i).toLowerCase(ch);
else
x += str.charAt(i).toUpperCase(ch);
}
console.log(x);
|
Approach : Using index() method
Java
public class GFG {
public static void main(String[] args)
{
String str = "GeEkSfOrGeEkS";
String x = "";
String upperalphabets
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String loweralphabets
= "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if (upperalphabets.indexOf(ch) != -1) {
x += loweralphabets.charAt(
upperalphabets.indexOf(ch));
}
else {
x += upperalphabets.charAt(
loweralphabets.indexOf(ch));
}
}
System.out.println(x);
}
}
|
Python3
str = "GeEkSfOrGeEkS"
x=""
upperalphabets="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
loweralphabets="abcdefghijklmnopqrstuvwxyz"
for i in str:
if i in upperalphabets:
x+=loweralphabets[upperalphabets.index(i)]
else:
x+=upperalphabets[loweralphabets.index(i)]
print(x)
|
C#
using System;
public class GFG {
public static void Main(string[] args)
{
string str = "GeEkSfOrGeEkS";
string x = "";
string upperalphabets
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string loweralphabets
= "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < str.Length; i++) {
char ch = str[i];
if (upperalphabets.IndexOf(ch) != -1) {
x += loweralphabets[upperalphabets.IndexOf(
ch)];
}
else {
x += upperalphabets[loweralphabets.IndexOf(
ch)];
}
}
Console.WriteLine(x);
}
}
|
Javascript
let str = "GeEkSfOrGeEkS";
let x = "";
let upperalphabets
= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
let loweralphabets
= "abcdefghijklmnopqrstuvwxyz";
for (let i = 0; i < str.length; i++) {
let ch = str[i];
if (upperalphabets.indexOf(ch) != -1) {
x += loweralphabets[upperalphabets.indexOf(ch)];
}
else {
x += upperalphabets[loweralphabets.indexOf(ch)];
}
}
console.log(x);
|
Please Login to comment...