Open In App

toupper() in C++

Last Updated : 04 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

In C++, the toupper() function converts a lowercase alphabet to an uppercase alphabet. It is a library function defined inside the ctype.h header file. If the character passed is a lowercase alphabet, then the toupper() function converts it to an uppercase alphabet. This function does not affect an uppercase character, special symbol, digits, or other ASCII characters.

Syntax of toupper() in C++

toupper(int ch);

Parameters of toupper() in C++

It accepts a single parameter:

  • ch: It is the character to be converted to uppercase.

Return Value of toupper() in C++

This function returns the ASCII value of the uppercase character corresponding to the ch. It is automatically converted to the character if assigned to the character variable. We can also manually typecast it to char using the below syntax:

char c = (char) toupper('a');

Examples of toupper() in C++

The following examples illustrates how we can use toupper() in various scenarios.

Example 1:

The below example demonstrates how we can convert a given lowercase alphabet to uppercase in C++.

C++
// C++ program to demonstrate
// how to use the toupper() function
#include <iostream>
using namespace std;

int main()
{
    char c = 'g';

    cout << c << " in uppercase is represented as = ";

    // toupper() returns an int value there for typecasting
    // with char is required
    cout << (char)toupper(c);

    return 0;
}

Output
g in uppercase is represented as = G

Example 2:

The below example demonstrates how we can convert a given string having lowercase alphabets to uppercase string in C++.

C++
// C++ program to convert a string to uppercase
// using toupper
#include <iostream>
using namespace std;

int main()
{
    // string to be converted to uppercase
    string s = "geeksforgeeks";

    for (auto& x : s) {
        x = toupper(x);
    }

    cout << s;
    return 0;
}

Output
GEEKSFORGEEKS

Note: If the character passed in the toupper() is an uppercase character, special symbol or a digit, then toupper() will return the character as it is without any changes.

Example 3:

The below example illustrates what happened when we use toupper() on uppercase character, special symbol or a digit in C++.

C++
// C++ program to demonstrate
// example of toupper() function.
#include <iostream>
using namespace std;

int main()
{

    string s = "Geeks@123";

    for (auto x : s) {

        cout << (char)toupper(x);
    }

    return 0;
}

Output
GEEKS@123




Like Article
Suggest improvement
Share your thoughts in the comments