📜  切换字符串中所有字符的程序

📅  最后修改于: 2022-05-13 01:57:08.484000             🧑  作者: Mango

切换字符串中所有字符的程序

给定一个字符串,任务是切换字符串的所有字符,即将大写转换为小写,反之亦然。

例子:

Input  : gfg
Output : GFG

Input  : aBc12#
Output : AbC12#

Input  : tu@kmiNi
Output : TU@KMInI

遍历给定的字符串,如果出现大写字符,则转为小写,小写字母转为大写。

C++
// C++ program to toggle all characters
#include
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='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:];
        else if (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


Javascript


C++
// C++ program to toggle all characters
#include
using namespace std;
 
void toggleChars(char str[])
{
    for (int i=0; str[i]!='\0'; i++)
    {
        if (isalpha(str[i])) {
          str[i] = (char)(str[i]^(1<<5));
        }
    }
}
 
// Driver code
int main()
{
    char str[] = "GeKf@rGeek$";
      cout << "String before toggle " << endl;
      cout << str << endl;
    toggleChars(str);
    cout << "String after toggle " << endl;
    cout << str << endl;
    return 0;
}
 
// This code is contributed by @kaustubhvats08


Java
// Java program to toggle all characters
 
class GFG
{
 
static void toggleChars(char str[])
{
    for (int i=0; i


Python3
# Python3 program to toggle all characters
def toggleChars(s):
    for i in range(len(s)):
      if s[i].isalpha():
        s = s[:i] + str(chr(ord(s[i])^(1<<5))) + s[i+1:]
    return s;
 
# Driver code
s = "GeKf@rGeek$";
print("String before toggle")
print(s)
s = toggleChars(s);
print("String after toggle")
print(s);
     
# This code is contributed by @kaustubhvats08


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 (Char.IsLetter(str[i])){
          str[i] = (char)(str[i]^(1<<5));
        }
    }
}
 
// Driver code
public static void Main(String[] args)
{
    char []str = "GeKf@rGeek$".ToCharArray();
    Console.WriteLine("String before toggle ");
    Console.WriteLine(String.Join("",str));
    toggleChars(str);
    Console.WriteLine("String after toggle ");
    Console.WriteLine(String.Join("",str));
}
}
 
// This code is contributed by @kaustubhvats08


Javascript


输出:

gEkF@RgEEK$

我们可以使用按位异或运算来切换给定字符串中的所有字符

正如我们所知,小写字符和大写字符大小写的 ascii 值仅相差 1 位(第 5 位)。我们可以简单地切换该位来切换字符串的字符大小写。

C++

// C++ program to toggle all characters
#include
using namespace std;
 
void toggleChars(char str[])
{
    for (int i=0; str[i]!='\0'; i++)
    {
        if (isalpha(str[i])) {
          str[i] = (char)(str[i]^(1<<5));
        }
    }
}
 
// Driver code
int main()
{
    char str[] = "GeKf@rGeek$";
      cout << "String before toggle " << endl;
      cout << str << endl;
    toggleChars(str);
    cout << "String after toggle " << endl;
    cout << str << endl;
    return 0;
}
 
// This code is contributed by @kaustubhvats08

Java

// Java program to toggle all characters
 
class GFG
{
 
static void toggleChars(char str[])
{
    for (int i=0; i

Python3

# Python3 program to toggle all characters
def toggleChars(s):
    for i in range(len(s)):
      if s[i].isalpha():
        s = s[:i] + str(chr(ord(s[i])^(1<<5))) + s[i+1:]
    return s;
 
# Driver code
s = "GeKf@rGeek$";
print("String before toggle")
print(s)
s = toggleChars(s);
print("String after toggle")
print(s);
     
# This code is contributed by @kaustubhvats08

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 (Char.IsLetter(str[i])){
          str[i] = (char)(str[i]^(1<<5));
        }
    }
}
 
// Driver code
public static void Main(String[] args)
{
    char []str = "GeKf@rGeek$".ToCharArray();
    Console.WriteLine("String before toggle ");
    Console.WriteLine(String.Join("",str));
    toggleChars(str);
    Console.WriteLine("String after toggle ");
    Console.WriteLine(String.Join("",str));
}
}
 
// This code is contributed by @kaustubhvats08

Javascript


输出
String before toggle 
GeKf@rGeek$
String after toggle 
gEkF@RgEEK$