Convert a character to uppercase in C++

This article will discuss different ways to convert a character to uppercase in C++.

Table of Contents

Convert a character to uppercase using toupper() function

C++ provides a function std::toupper(char c) to get a uppercase equivalent of a character. It accepts a character as an argument, and if this character is lowercase, it returns an uppercase equivalent of that character. We can use this function to convert a character to uppercase in C++. For example,

#include <iostream>

int main()
{
    char ch = 'i';
 
    // Convert a character to uppercase
    ch = std::toupper(ch);

    std::cout << ch <<std::endl;

    return 0;
}

Output:

I

It converted the lowercase character ‘i’ to an uppercase character ‘I’. If the provided character is not lowercase, then std::toupper() will return the same character. For example,

Advertisements
#include <iostream>

int main()
{
    char ch = '+';
 
    // Convert a character to uppercase
    ch = std::toupper(ch);

    std::cout << ch <<std::endl;

    return 0;
}

Output:

+

The provided character is not a lowercase character; therefore toupper() returned the same character.

Convert a character to uppercase by using the ASCII values

Every character in C++ has an ASCII value associated with it. In C++, the ASCII values of uppercase characters (i.e. ‘A’ to ‘Z’) in C++ are 65 to 90. The ASCII values of lowercase characters (i.e. ‘a’ to ‘z’) in C++ are 97 to 122. So, to convert a lowercase character to uppercase, we can subtract 32 from the character(its ASCII value). When we subtract anything from a character, the character gets automatically converted to an int (i.e. the ascii value), and the value is subtracted. We can use this logic to convert a character to uppercase.

The steps are as follows,

  • Check if the provided character contains an alphabet and a lowercase character.
  • If yes, then subtract 32 from the character.

For example,

#include <iostream>

int main()
{
    char ch = 'i';

    // Check if char is a lowercase alphabet
    if( isalpha(ch) && islower(ch) )
    {
        // Convert a character to uppercase
        ch = ch - 32;
    }

    std::cout << ch <<std::endl;

    return 0;
}

Output:

I

It converted the lowercase character ‘i’ to an uppercase character ‘I’. Let’s check a negative example and convert the character ‘+’ to uppercase.

For example,

#include <iostream>

int main()
{
    char ch = '+';

    // Check if char is a lowercase alphabet
    if( isalpha(ch) && islower(ch) )
    {
        // Convert a character to uppercase
        ch = ch - 32;
    }

    std::cout << ch <<std::endl;

    return 0;
}

Output:

+

The provided character is not a lowercase character; therefore, our code didn’t change its value.

Summary:

We learned about two different ways to convert a character to uppercase in C++.

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top