Program to toggle all characters in a string

in a given string in which we toggle all characters.

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


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

Improved By : 29AjayKumar, princi singh



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.