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.

Case conversion
Case conversion

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 input of type char, and assign a to it.
  • Line 7: Use toupper() to convert input character 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 input of type char, and assigning A to it.
  • Line 7: Use tolower() to convert input character to lowercase.
  • Line 8: Print the result.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved