Dada una string, convierta los caracteres de la string en mayúsculas y minúsculas, es decir, si un carácter está en minúsculas, conviértalo en mayúsculas y viceversa.
Ejemplos:
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>
PHP
<?php // Function to convert characters // of a string to opposite case function convertOpposite($str) { $ln = strlen($str); // Conversion according to ASCII values for ($i = 0; $i < $ln; $i++) { if ($str[$i] >= 'a' && $str[$i] <= 'z'){ // Convert lowercase to uppercase $str[$i] = strtoupper($str[$i]); } else if ($str[$i] >= 'A' && $str[$i] <= 'Z'){ // Convert uppercase to lowercase $str[$i] = strtolower($str[$i]); } } return $str; } // Driver function $str = "GeEkSfOrGeEkS"; // Calling the Function echo(convertOpposite($str)); // This code is contributed by ksrikanth0498 ?>
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 " + "\n"); System.out.print(String.valueOf(S)); } } // This code is contributed by Amit Katiyar
Python3
# python program for the same approach def isalpha(input): input_char = ord(input[0]) # CHECKING FOR ALPHABET if((input_char >= 65 and input_char <= 90) or (input_char >= 97 and input_char <= 122)): return True else: return False # Function to toggle characters 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 # Driver code S = "GeKf@rGeek$" print(f"String after toggle {toggleChars(S)}") # This code is contributed by shinjanpatra
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 " + "\n"); 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
Python3
# Python3 program to Convert characters # of a string to opposite case str = "GeEkSfOrGeEkS" x="" for i in str: if(i.isupper()): x+=i.lower() else: x+=i.upper() print(x)
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA