A string is a sequence of characters. In this article, you'll learn how to convert the characters of a string to the opposite cases. You'll also learn how to solve this problem using the most popular programming languages like C++, Python, C, and JavaScript.
Problem Statement
You're given a string. You need to convert all the characters of this string to the opposite cases.
Example 1: Let str = "Welcome to MUO"
String after converting all the characters to the opposite cases = "wELCOME TO muo"
Thus, the output is "wELCOME TO muo".
Example 2: Let str = "Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair."
String after converting all the characters to the opposite cases = "fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR."
Thus, the output is "fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.".
Example 3: Let str = "Tom threw Tim three thumbtacks"
String after converting all the characters to the opposite cases = "tOM THREW tIM THREE THUMBTACKS"
Thus, the output is "tOM THREW tIM THREE THUMBTACKS".
C++ Program to Convert Characters of a String to the Opposite Cases
Below is the C++ program to convert the characters of a string to the opposite cases:
// C++ program to convert characters of string to opposite case
#include <iostream>
using namespace std;
string convertString(string& str)
{
int length = str.length();
for (int i = 0; i < length; i++)
{
// If the character is in lowercase,
// convert it to uppercase
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
// If the character is in uppercase,
// convert it to lowercase
else if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
}
return str;
}
int main()
{
string str1 = "Welcome to MUO";
cout << "Original String 1:" << endl;
cout << str1 << endl;
str1 = convertString(str1);
cout << "Converted String 1:" << endl;
cout << str1 << endl;
string str2 = "Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.";
cout << "Original String 2:" << endl;
cout << str2 << endl;
str2 = convertString(str2);
cout << "Converted String 2:" << endl;
cout << str2 << endl;
string str3 = "Tom threw Tim three thumbtacks";
cout << "Original String 3:" << endl;
cout << str3 << endl;
str3 = convertString(str3);
cout << "Converted String 3:" << endl;
cout << str3 << endl;
return 0;
}
Output:
Original String 1:
Welcome to MUO
Converted String 1:
wELCOME TO muo
Original String 2:
Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.
Converted String 2:
fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.
Original String 3:
Tom threw Tim three thumbtacks
Converted String 3:
tOM THREW tIM THREE THUMBTACKS
Python Program to Convert Characters of a String to the Opposite Cases
Below is the Python program to convert the characters of a string to the opposite cases:
# Python program to convert characters of string to opposite case
def convertString(str):
length = len(str)
result = ""
for i in range(length):
# If the character is in lowercase,
# convert it to uppercase
if str[i].islower():
result += str[i].upper()
# If the character is in uppercase,
# convert it to lowercase
elif str[i].isupper():
result += str[i].lower()
else:
result += str[i]
return result
str1 = "Welcome to MUO"
print("Original String 1:")
print(str1)
print("Converted String 1:")
print(convertString(str1))
str2 = "Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair."
print("Original String 2:")
print(str2)
print("Converted String 2:")
print(convertString(str2))
str3 = "Tom threw Tim three thumbtacks"
print("Original String 3:")
print(str3)
print("Converted String 3:")
print(convertString(str3))
Output:
Original String 1:
Welcome to MUO
Converted String 1:
wELCOME TO muo
Original String 2:
Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.
Converted String 2:
fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.
Original String 3:
Tom threw Tim three thumbtacks
Converted String 3:
tOM THREW tIM THREE THUMBTACKS
JavaScript Program to Convert Characters of a String to the Opposite Cases
Below is the JavaScript program to convert the characters of a string to the opposite cases:
// JavaScript program to convert characters of string to opposite case
function convertString(str) {
var length = str.length;
var result = "";
for (let i = 0; i < str.length; i++) {
// If the character is in lowercase,
// convert it to uppercase
if (str.charAt(i) === str.charAt(i).toLowerCase()) {
result += str.charAt(i).toUpperCase();
// If the character is in uppercase,
// convert it to lowercase
} else if (str.charAt(i) === str.charAt(i).toUpperCase()) {
result += str.charAt(i).toLowerCase()
} else {
result += str.charAt(i);
}
}
return result;
}
var str1 = "Welcome to MUO";
document.write("Original String 1:" + "
");
document.write(str1 + "
");
str1 = convertString(str1);
document.write("Converted String 1:" + "
");
document.write(str1 + "
");
var str2 = "Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.";
document.write("Original String 2:" + "
");
document.write(str2 + "
");
str2 = convertString(str2);
document.write("Converted String 2:" + "
");
document.write(str2 + "
");
var str3 = "Tom threw Tim three thumbtacks";
document.write("Original String 3:" + "
");
document.write(str3 + "
");
str3 = convertString(str3);
document.write("Converted String 3:" + "
");
document.write(str3 + "
");
Output:
Original String 1:
Welcome to MUO
Converted String 1:
wELCOME TO muo
Original String 2:
Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.
Converted String 2:
fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.
Original String 3:
Tom threw Tim three thumbtacks
Converted String 3:
tOM THREW tIM THREE THUMBTACKS
C Program to Convert Characters of a String to the Opposite Cases
Below is the C program to convert the characters of a string to the opposite cases:
// C program to convert characters of string to opposite case
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
const char* convertString(char str[])
{
int length = strlen(str);
for (int i = 0; i < length; i++)
{
// If the character is in lowercase,
// convert it to uppercase
if (str[i] >= 'a' && str[i] <= 'z')
{
str[i] = str[i] - 32;
}
// If the character is in uppercase,
// convert it to lowercase
else if (str[i] >= 'A' && str[i] <= 'Z')
{
str[i] = str[i] + 32;
}
}
return str;
}
int main()
{
char str1[] = "Welcome to MUO";
printf("Original String 1: \n");
printf("%s \n", str1);
printf("Converted String 1: \n");
printf("%s", convertString(str1));
char str2[] = "Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.";
printf("Original String 2: \n");
printf("%s \n", str2);
printf("Converted String 2: \n");
printf("%s", convertString(str2));
char str3[] = "Tom threw Tim three thumbtacks";
printf("Original String 3: \n");
printf("%s \n", str3);
printf("Converted String 3: \n");
printf("%s", convertString(str3));
return 0;
}
Output:
Original String 1:
Welcome to MUO
Converted String 1:
wELCOME TO muo
Original String 2:
Fuzzy Wuzzy was a bear. Fuzzy Wuzzy had no hair.
Converted String 2:
fUZZY wUZZY WAS A BEAR. fUZZY wUZZY HAD NO HAIR.
Original String 3:
Tom threw Tim three thumbtacks
Converted String 3:
tOM THREW tIM THREE THUMBTACKS
Learn More About String Manipulation
In this article, you learned how to convert characters of the string to opposite cases. Dealing with strings and texts is an integral part of programming. You must know how to manipulate strings.
Python is a solid choice to get started with if you're looking for a language to manipulate strings easily and efficiently.