How to perform case conversion in C++
What is case conversion?
Case conversion refers to converting a letter from one case to another, such as converting a lowercase letter to uppercase or vice versa. The C++ standard library provides several functions for performing case conversion, including:
-
toupper(char): This function converts a single character to its uppercase equivalent. If the input character is already uppercase, it is returned unmodified. The function takes a char argument and converts that character to uppercase. -
tolower(char): This function converts a single character to its lowercase equivalent. If the input character is already lowercase, it is returned unmodified. The function takes a char argument and converts that character to lowercase.
Example of using toupper
The following code shows how we can convert a character from lowercase to uppercase:
#include <iostream>using namespace std;int main(){char input = 'a';char converted_input = toupper(input);cout << "The uppercase of " << input << " is " << converted_input << endl;return 0;}
Explanation
- Line 6: Declare a variable name
inputof typechar, and assignato it. - Line 7: Use
toupper()to convertinputcharacter to uppercase. - Line 8: Print the result.
Example of using tolower
The following code shows how we can convert a character from uppercase to lowercase:
#include <iostream>using namespace std;int main(){char input = 'A';char converted_input = tolower(input);cout << "The lowercase of " << input << " is " << converted_input << endl;return 0;}
Explanation
- Line 6: Declare a variable name
inputof typechar, and assigningAto it. - Line 7: Use
tolower()to convertinputcharacter to lowercase. - Line 8: Print the result.
Free Resources