Related Articles
Convert characters of a string to opposite case
• Difficulty Level : Easy
• Last Updated : 29 Apr, 2020

Given a string, convert the characters of the string into opposite case,i.e. if a character is lower case than convert it into upper case and vice-versa.
Examples:

```Input : geeksForgEeks
Output : GEEKSfORGeEKS

Input : hello every one
Output : HELLO EVERY ONE
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

ASCII values  of alphabets: A – Z = 65 to 90, a – z = 97 to 122
Steps:

1. Take one string of any length and calculate its length.
2. Scan string character by character and keep checking  the index .
• If character in a index is in lower case, then subtract 32 to convert it in upper case, else add 32 to convert it in lower case
3. Print the final string.

## C++

 `// CPP program to Convert characters  ` `// of a string to opposite case ` `#include ` `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=``'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

## 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=``'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 `

Output:

```gEeKsFoRgEeKs
```

Time Complexity: O(n)

Note:
This program can alternatively be done using C++ inbuilt functions – Character.toLowerCase(char) and Character.toUpperCase(char).

This article is contributed by Rishabh Jain. 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.