askvity

How Do I Change the Case of a Character in CPP?

Published in Character Case Conversion Cpp 2 mins read

To change the case of a character in C++, you can use standard library functions provided in the <cctype> header. The two primary functions for case conversion are tolower() and toupper().

These functions take a character as input and return the corresponding character in the desired case. If the input character is not a letter of the appropriate case, or not a letter at all, the original character is returned unchanged.

Using tolower()

The tolower() function converts an uppercase character to its lowercase equivalent.

Syntax:

int tolower(int ch);

Description:

  • Takes an integer argument ch, which represents the character.
  • If ch is an uppercase letter, it returns the lowercase equivalent.
  • Otherwise, it returns ch unchanged.

Example:

Let's demonstrate how to convert an uppercase character to lowercase using tolower(). This example incorporates elements shown in the provided reference, such as including <iostream>, using namespace std;, declaring a char input = 'A';, and using cout.

#include <iostream> // Included as shown in reference
#include <cctype>   // Required for tolower()

using namespace std; // Used as shown in reference

int main()
{
    char input = 'A'; // Declared as shown in reference
    char converted_input = tolower(input); // Using tolower() to convert 'A'

    cout << "The lowercase of " << input << " is " << converted_input << endl; // Outputting the result

    char another_input = 'b';
    char converted_another = tolower(another_input);
    cout << "The lowercase of " << another_input << " is " << converted_another << endl; // 'b' remains 'b'

    char non_alpha_input = '5';
    char converted_non_alpha = tolower(non_alpha_input);
    cout << "The lowercase of " << non_alpha_input << " is " << converted_non_alpha << endl; // '5' remains '5'

    return 0; // Returned 0 as shown in reference
}

Output:

The lowercase of A is a
The lowercase of b is b
The lowercase of 5 is 5

Using toupper()

The toupper() function converts a lowercase character to its uppercase equivalent.

Syntax:

int toupper(int ch);

Description:

  • Takes an integer argument ch, which represents the character.
  • If ch is a lowercase letter, it returns the uppercase equivalent.
  • Otherwise, it returns ch unchanged.

Example:

Here's how to convert a lowercase character to uppercase using toupper().

#include <iostream>
#include <cctype> // Required for toupper()

using namespace std;

int main()
{
    char input = 'z';
    char converted_input = toupper(input); // Using toupper() to convert 'z'

    cout << "The uppercase of " << input << " is " << converted_input << endl;

    char another_input = 'B';
    char converted_another = toupper(another_input);
    cout << "The uppercase of " << another_input << " is " << converted_another << endl; // 'B' remains 'B'

    return 0;
}

Output:

The uppercase of z is Z
The uppercase of B is B

Essential Header File

Both tolower() and toupper() are declared in the <cctype> header file. You must include this header at the beginning of your C++ source file to use these functions.

  • #include <cctype>: This is the preferred C++-style header.
  • #include <ctype.h>: This is the C-style header, also available in C++.

Summary of Case Conversion Functions

Here's a quick overview of the functions:

Function Description Header Required
tolower Converts uppercase char to lowercase. <cctype>
toupper Converts lowercase char to uppercase. <cctype>

By using these standard library functions, you can easily perform case conversions for individual characters in your C++ programs.

Related Articles