Given a string, convert the characters of the string into opposite case,i.e. if a character is lower case then convert it into upper case and vice-versa.
Examples:
Input : geeksForgEeks
Output : GEEKSfORGeEKS
Input : hello every one
Output : HELLO EVERY ONE
Time Complexity: O(n) Note: This program can alternatively be done using C++ inbuilt functions – Character.toLowerCase(char) and Character.toUpperCase(char).
Approach 2: The problem can be solved using letter case toggling. Follow the below steps to solve the problem:
Traverse the given string S.
For each character Si, do Si = Si^ (1 << 5).
Si^ (1 << 5) toggles the 5thbit which means 97 will become 65 and 65 will become 97:
65 ^ 32 = 97
97 ^ 32 = 65
Print the string after all operations
Below is the implementation of the above approach:
document.write( "String after toggle "+toggleChars(S));
</script>
// This code is contributed by Akshit Saxena
This article is contributed by Rishabh Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.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.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy