Change case of each character in a string

Problem Statement: Write a  program to change the case (lower to upper and upper to lower cases) of each character of a given string.

Examples:

Example 1:
Input: String str = “javA”
Output: JAVa
Explanation:
 Changed the lower case characters to uppercase and vice versa.

Example 2:
Input: String str = “take u forward IS Awesome”
Output: TAKE U FORWARD is aWESOME
Explanation: Changed the lower case characters to uppercase and vice versa.

Solution

Disclaimer: Don’t jump directly to the solution, try it out yourself first.

Solution 1:

Approach

  • Keep on iterating the given string:
  • If the corresponding character is uppercase then convert it to lowercase using ascii value by adding 32 to its current ascii value and then typecasting it.
  • Else if the character is lowercase then convert it to uppercase using ascii value by subtracting 32 from its current ascii value and then typecasting it.
  • If any whitespace then keep it as it is.

Return the final string

Code:

C++ Code

#include<bits/stdc++.h>

using namespace std;
void solve(string str, int n) {

  for (int i = 0; i < n; i++) {
    int ascii = (int) str[i]; // calculating ascii value

    if (ascii >= 65 && ascii <= 90) // if uppercase
      str[i] = ((char)(ascii + 32));

    else if (ascii >= 97 && ascii <= 122) // if lowercase
      str[i] = ((char)(ascii - 32));

  }

  cout << "Resultant string: " << "\n";
  cout << str << "\n";
}
int main() {
  // Input string
  string str = "take u forward IS Awesome";
  int n = str.length();

  solve(str, n);
  return 0;
}

Output:

Resultant string:
TAKE U FORWARD is aWESOME

Time Complexity: O(n)

Space Complexity: O(1)

Java Code

public class tUf {
  public static String solve(String str, int n) {
    StringBuffer ans = new StringBuffer();
    for (int i = 0; i < n; i++) {
      int ascii = (int) str.charAt(i);

      if (ascii >= 65 && ascii <= 90)
        ans.append((char)(ascii + 32));

      else if (ascii >= 97 && ascii <= 122)
        ans.append((char)(ascii - 32));

      else if (str.charAt(i) == ' ')
        ans.append(' ');
    }

    return ans.toString();
  }
  public static void main(String args[]) {
    String str = "take u forward IS Awesome";
    int length = str.length();

    System.out.println("Resultant string: ");
    System.out.println(solve(str, length));
  }
}

Output:

Resultant string:
TAKE U FORWARD is aWESOME

Time Complexity: O(n)

Space Complexity: O(n), for StringBuffer

Solution 2: Using built-in functions

Approach:

  • Keep on iterating the given string:
  • If the corresponding character is uppercase then convert it to lowercase using built in functions described below
  • Else if the character is lowercase then convert it to uppercase using built in functions mentioned below.
  • If any whitespace then keep it as it is.
  • Return the final string

In C++,

towupper(): converts wide characters into uppercase

towlower(): converts wide characters into lowercase

In Java,

Character.toUpperCase(): converts the character argument to uppercase 

Character.toLowerCase(): converts the character argument to lowercase

Code:

C++ Code

#include<bits/stdc++.h>

using namespace std;
void solve(string str, int n) {

  for (int i = 0; i < n; i++) {
    int ascii = (int) str[i];

    if (ascii >= 65 && ascii <= 90) // if uppercase
      str[i] = towlower(str[i]);

    else if (ascii >= 97 && ascii <= 122) // if lowercase
      str[i] = towupper(str[i]);

  }

  cout << "Resultant string: " << "\n";
  cout << str << "\n";
}
int main() {

  // Input string
  string str = "take u forward IS Awesome";
  int n = str.length();

  solve(str, n);
  return 0;
}

Output:

Resultant string:
TAKE U FORWARD is aWESOME

Time Complexity: O(n)

Space Complexity: O(1)

Java Code

public class tUf {
  public static String solve(String str, int n) {
    StringBuffer ans = new StringBuffer();
    for (int i = 0; i < n; i++) {
      int ascii = (int) str.charAt(i);

      if (ascii >= 65 && ascii <= 90) // if uppercase
        ans.append(Character.toLowerCase((char) ascii));

      else if (ascii >= 97 && ascii <= 122) // if lowercase
        ans.append(Character.toUpperCase((char) ascii));

      else if (str.charAt(i) == ' ') // if whitespace
        ans.append(' ');
    }

    return ans.toString();
  }
  public static void main(String args[]) {
    String str = "take u forward IS Awesome";
    int length = str.length();

    System.out.println("Resultant string: ");
    System.out.println(solve(str, length));
  }
}

Output:

Resultant string:
TAKE U FORWARD is aWESOME

Time Complexity: O(n)

Space Complexity: O(1)

Special thanks to Rishiraj Girmal for contributing to this article on takeUforward. If you also wish to share your knowledge with the takeUforward fam, please check out this article