Skip to content
Related Articles

Related Articles

Program to toggle all characters in a string
  • Difficulty Level : Easy
  • Last Updated : 23 Oct, 2020

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 comes, convert into lowercase and lowercase latter convert into uppercase.  

C++




// C++ program to toggle all characters
#include<bits/stdc++.h>
using namespace std;
 
void toggleChars(char str[])
{
    for (int i=0; str[i]!='\0'; i++)
    {
        if (str[i]>='A' && str[i]<='Z')
            str[i] = str[i] + 'a' - 'A';
        else if (str[i]>='a' && str[i]<='z')
            str[i] = str[i] + 'A' - 'a';
    }
}
 
// Driver code
int main()
{
    char str[] = "GeKf@rGeek$";
    toggleChars(str);
    cout << "String after toggle " << endl;
    cout << str << endl;
    return 0;
}

Java




// Java program to toggle all characters
 
class GFG
{
 
static void toggleChars(char str[])
{
    for (int i=0; i<str.length; i++)
    {
        if (str[i]>='A' && str[i]<='Z')
            str[i] = (char) (str[i] + 'a' - 'A');
        else if (str[i]>='a' && str[i]<='z')
            str[i] = (char) (str[i] + 'A' - 'a');
    }
}
 
// Driver code
public static void main(String[] args)
{
    char str[] = "GeKf@rGeek$".toCharArray();
    toggleChars(str);
    System.out.println("String after toggle ");
    System.out.println(String.valueOf(str));
}
}
 
// This code is contributed by 29AjayKumar

Python3




# Python3 program to toggle all characters
def toggleChars(str):
 
    for i in range(len(str)):
        if (str[i] >= 'A' and str[i] <= 'Z'):
            str = str[:i] + chr(ord(str[i]) + \
                     ord('a') - ord('A')) + str[i + 1:];
        elif (str[i] >= 'a' and str[i] <= 'z'):
            str = str[:i] + chr(ord(str[i]) + \
                     ord('A') - ord('a')) + str[i + 1:];
    return str;
 
# Driver code
str = "GeKf@rGeek$";
str = toggleChars(str);
print(str);
     
# This code is contributed by 29AjayKumar

C#




// C# program to toggle all characters
using System;
     
class GFG
{
 
static void toggleChars(char []str)
{
    for (int i = 0; i < str.Length; i++)
    {
        if (str[i] >= 'A' && str[i] <= 'Z')
            str[i] = (char) (str[i] + 'a' - 'A');
        else if (str[i] >= 'a' && str[i] <= 'z')
            str[i] = (char) (str[i] + 'A' - 'a');
    }
}
 
// Driver code
public static void Main(String[] args)
{
    char []str = "GeKf@rGeek$".ToCharArray();
    toggleChars(str);
    Console.WriteLine("String after toggle ");
    Console.WriteLine(String.Join("",str));
}
}
 
// This code is contributed by Princi Singh

Output: 

gEkF@RgEEK$

This article is contributed by MATHE_KA_BANDA. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :