Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Understanding the toupper() Function
3.
An Example of the toupper() Function
4.
Key Points about the toupper() Function
5.
Frequently Asked Questions
5.1.
Can toupper() handle strings?
5.2.
Does toupper() modify the original character?
5.3.
Can toupper() convert special characters or numbers?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

toupper() in C++

Author Gunjan Batra
0 upvote

Introduction

In C++, there are a plethora of built-in functions that significantly simplify coding tasks. Among these functions, the ones designed to manipulate characters and strings are quite useful. One such function is toupper(), a function designed to convert lowercase letters to uppercase. 

toupper() in C++

 

This article will explore the toupper() function in C++, discussing its use, demonstrating how it works, and addressing common questions about it.

 

Understanding the toupper() Function

The toupper() function is a standard library function in C++ that converts a lowercase letter to its uppercase equivalent. If the passed character is already in uppercase, or is not an alphabetic character, the function returns the character as it is.

Here is the syntax of the toupper() function:

int toupper(int c);

The toupper() function resides in the <cctype> header file, so you'll need to include it in your program to use the function:

#include <cctype>

An Example of the toupper() Function

Here's an illustrative example of the toupper() function:

#include <iostream>
#include <cctype>


int main() {
    char ch = 'g';
    
    std::cout << "Original character: " << ch << '\n';
    
    char upper_ch = toupper(ch);
    
    std::cout << "After toupper: " << upper_ch << '\n';
    
    return 0;
}

Output

In the above program, the toupper() function is used to convert the lowercase letter 'g' to uppercase. The output of the program would be:

output

Key Points about the toupper() Function

When using toupper(), it's important to keep the following points in mind:

  • Argument: The toupper() function takes a single argument - an integer or a character to convert to uppercase.
     
  • Return Value: It returns the uppercase equivalent if the argument is a lowercase letter. Otherwise, it returns the argument as it is.
     
  • Non-Alphabetic Characters: If a non-alphabetic character is passed to toupper(), it will return the character unmodified.
     
  • Library: toupper() is part of the <cctype> library in C++.


Also see, Abstract Data Types in C++

Frequently Asked Questions

Can toupper() handle strings?

No, toupper() works on individual characters. To convert a whole string, you'd need to iterate over the string and convert each character individually.

Does toupper() modify the original character?

No, toupper() returns the converted character but does not modify the original one.

Can toupper() convert special characters or numbers?

No, toupper() only converts lowercase alphabetic characters to uppercase. Other characters are returned as they are.

Conclusion

The toupper() function in C++ is a handy tool when working with text data, especially when there's a need to convert characters to uppercase. It simplifies what could be a tedious task if done manually and contributes to writing more efficient and clean code. As with all tools, understanding its usage, strengths, and limitations is key to using it effectively. Happy coding!

Also read -  File Handling in CPP

Live masterclass