给定一个字符串,将该字符串的字符转换为相反的大小写,即如果一个字符是小写,则将其转换为大写,反之亦然。
null
例如:
Input : geeksForgEeksOutput : GEEKSfORGeEKSInput : hello every oneOutput : HELLO EVERY ONE
字母表的ASCII值:A–Z=65到90,A–Z=97到122 步骤:
- 取任意长度的字符串,计算其长度。
- 逐个字符扫描字符串,并不断检查索引。
- 如果索引中的某个字符是小写的,则减去32将其转换为大写,否则加上32将其转换为小写
- 打印最后一个字符串。
C++
// CPP program to Convert characters // of a string to opposite case #include <iostream> using namespace std; // Function to convert characters // of a string to opposite case void convertOpposite(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; } } // Driver function int main() { string str = "GeEkSfOrGeEkS" ; // Calling the Function convertOpposite(str); cout << str; return 0; } |
JAVA
// Java program to Convert characters // of a string to opposite case class Test { // Method to convert characters // of a string to opposite case static void convertOpposite(StringBuffer str) { int ln = str.length(); // Conversion using predefined methods 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" ); // Calling the Method convertOpposite(str); System.out.println(str); } } // This code is contributed by Gaurav Miglani |
Python3
# Python3 program to Convert characters # of a string to opposite case # Function to convert characters # of a string to opposite case def convertOpposite( 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 ) # Driver code if __name__ = = "__main__" : str = "GeEkSfOrGeEkS" str = list ( str ) # Calling the Function convertOpposite( str ) str = ''.join( str ) print ( str ) # This code is contributed by # sanjeev2552 |
C#
// C# program to Convert characters // of a string to opposite case using System; using System.Text; class GFG{ // Method to convert characters // of a string to opposite case static void convertOpposite(StringBuilder 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] = ( char )(str[i] - 32); else if (str[i]>= 'A' && str[i]<= 'Z' ) //Convert uppercase to lowercase str[i] = ( char )(str[i] + 32); } } // Driver code public static void Main() { StringBuilder str = new StringBuilder( "GeEkSfOrGeEkS" ); // Calling the Method convertOpposite(str); Console.WriteLine(str); } } // This code is contributed by PrinciRaj1992 |
Javascript
<script> // Function to convert characters // of a string to opposite case function convertOpposite(str) { var ln = str.length; // Conversion according to ASCII values for ( var i = 0; i < ln; i++) { if (str[i] >= 'a' && str[i] <= 'z' ) // Convert lowercase to uppercase document.write( String.fromCharCode(str.charCodeAt(i) - 32) ); else if (str[i] >= 'A' && str[i] <= 'Z' ) // Convert uppercase to lowercase document.write( String.fromCharCode(str.charCodeAt(i) + 32) ); } } // Driver function var str = "GeEkSfOrGeEkS" ; // Calling the Function convertOpposite(str); </script> |
输出
gEeKsFoRgEeKs
时间复杂性 :O(n) 注: 这个程序可以用C++内置函数——字符来完成。toLowerCase(char)和Character。toUpperCase(char)。
方法2: 这个问题可以通过使用 字母盒切换 .按照以下步骤解决问题:
- 遍历给定的字符串 s
- 每个角色 s 我 做 s 我 =S 我 ^ (1 << 5).
- s 我 ^ (1 << 5) 切换 5. th 这意味着 97 将成为 65 和 65 将成为 97:
- 65 ^ 32 = 97
- 97 ^ 32 = 65
- 在所有操作之后打印字符串
以下是上述方法的实施情况:
C++
// C++ program to toggle all characters #include<bits/stdc++.h> using namespace std; // Function to toggle characters void toggleChars(string &S) { for ( auto &it : S){ if ( isalpha (it)){ it ^= (1 << 5); } } } // Driver code int main() { string S = "GeKf@rGeek$" ; toggleChars(S); cout << "String after toggle " << endl; cout << S << endl; return 0; } //Code contributed by koulick_sadhu |
JAVA
// Java program to toggle all characters import java.util.*; class GFG{ static char []S = "GeKf@rGeek$" .toCharArray(); // Function to toggle characters static void toggleChars() { for ( int i = 0 ; i < S.length; i++) { if (Character.isAlphabetic(S[i])) { S[i] ^= ( 1 << 5 ); } } } // Driver code public static void main(String[] args) { toggleChars(); System.out.print( "String after toggle " + "" ); System.out.print(String.valueOf(S)); } } // This code is contributed by Amit Katiyar |
C#
// C# program to toggle all characters using System; class GFG{ static char []S = "GeKf@rGeek$" .ToCharArray(); // Function to toggle characters static void toggleChars() { for ( int i = 0; i < S.Length; i++) { if ( char .IsLetter(S[i])) { S[i] = ( char )(( int )(S[i]) ^ (1 << 5)); } } } // Driver code public static void Main(String[] args) { toggleChars(); Console.Write( "String after toggle " + "" ); Console.Write(String.Join( "" , S)); } } // This code is contributed by Princi Singh |
Javascript
<script> function isalpha(input) { var input_char = input.charCodeAt(0); // CHECKING FOR ALPHABET if ( (input_char >= 65 && input_char <= 90) || (input_char >= 97 && input_char <= 122)) { return true ; } else { return false ; } } // Function to toggle characters 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; } // Driver code var S = "GeKf@rGeek$" ; document.write( "String after toggle " +toggleChars(S)); </script> // This code is contributed by Akshit Saxena |
本文由 里沙布·贾因 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END