C++

C++ toupper()

You may have heard about the very well-known type-casting concept in C++ programming. Typecasting allows you to convert a character or string to another case, i.e. upper and lower. C++ came up with the toupper() function of the “cctype” library to convert a string or character to uppercase. Typecasting becomes easy with these functions, i.e. toupper() and tolower(). Therefore, we came up with this tutorial to elaborate the concept of the toupper() method of C++ while working within Ubuntu 20.04. Thus, we have started this guide by creating a new C++ document and opening it within some editor, i.e. nano.

Example 01:

Let’s get started with the very first and simplest example of this tutorial guide. So, we have started the code of C++ with some libraries needed to make this code executable and working properly. Thus, the “#include’ keyword has been utilized to include the “iostream” for input-output and “cctype” to use different C++ type functions in the code. After all the libraries, we have to castoff “std” for the standard namespace of c++ in the code to avoid using the word “std” again and again with every input and output clause.

This C++ code starts from the main() method to start the execution. The character type variable named “v” has been declared and assigned with a character value “m”. This value has been directly converted into upper-case using the C++ type-casting method named “toupper()” and saved to the variable “v”. A very next “cout” statement is castoff to display the directly converted “upper-case” value “v” on the terminal display of Ubuntu 20.04. The “endl” has been used within the “cout” clause to provide a new line or jump to the next line. The return 0 status indicates that this function will return nothing, and the code is ended here with success.

Here comes the everlasting “g++” Compiler for C++ to compile our newly made code in a terminal of the Ubuntu 20.404 system. Therefore, the file saved in the home folder has been compiled using the g++ instruction and got worked perfectly fine. The code has got the converted character in the upper-case on the shell via the “./a.out” command.

Example 02:

The first example was converting a single lower-case character to an upper-case with the toupper() function in C++. We will also apply the toupper() function to numbers in this example. So, the code has been started with the same input-output header and cctype header for the type-casting function. After the standard “Std” namespace, the main() execution method has been started with the simple initialization of 4 character type variables v1, v2, v3, and v4. You can see that the first two variables are characters, but the next two variables are numbers used as characters.

After the initialization, we have implemented 4 cout statements to display the converted characters on a terminal screen. But, here comes the difference in the conversion of characters to upper-case. Within the first two “cout” statements, we have converted character variables to having character value in them to the upper-case. The “toupper()” has been applied to the variables v1 and v2. The only difference is the use of char type before the toupper() function on variable v2, while the first cout statement is simple. The same way has been applied to the variables v3 and v4 to convert character type “number” to upper-case. The main() function ends here, and we are good to execute this code.

After the new code compilation, we have got no errors in return. We have got quite dissimilar output on our shell on the execution of this code. The output for the first character variable is integer 65, and for the next character, it’s the uppercase “C” character. The same has been applied to the next two number-type character variables. The characters converted to upper-case are due to usage of “(char)” with toupper() method. While, without using char type on toupper(), we have got the “numbers” in return after the upper-case conversion.

Example 03:

Let’s move towards the next example from a lower-case string to an upper-case string. The code has been started with the same headers and namespace. The only difference is using “cstring” to use string-related functions in C++ code. After the creation of the main() execution function, we have declared a character-type string variable “val” containing a string value in it, i.e. “Linux Programming”. The cout statement is here to display the original string as it is on the terminal. The next cout clause shows us that we will convert a lower-case string to an upper-case using the “for” loop in the code. So, the “for” loop has been initiated from index 0 to the length of string “val”. Each character of a string “val” has been converted into upper-case using the string indexes and saved to the character variable “n” each time. Every character has been displayed on the shell using a cout statement. After the “for” loop ends, the line break has been added, and the program is complete now.

The code has been compiled first and then executed with the “./a.out” command. The original string has been displayed first, then the converted upper-case string.

Example 04:

Let’s have the last but not the least example to convert the string variable into upper-case using the toupper() function of the C++ string library. This code has been started again with the same libraries and namespace of C++, i.e. std, iostream, cstring, and cctype. Within the main() function, an integer type variable “I” is assigned a value 0 that will be used as an index to the string variable. Next, we have initialized a character-type string variable “val” with string value in it with numbers. This original string “val” has been displayed on the shell through the “cout” clause.

The next cout clause is telling us that now we are going to convert and display the original string to upper-case. Now, the “while” loop has been started to check until the last index of variable “val”, and each character has been saved to the new variable “n” of character type. The toupper() function has been applied to the variable “n” to convert into upper-case. While the putchar() function has been applied to the character to display each character on the shell directly. The index of a string will be incremented on each character conversion, i.e. i++. After a while, a loop has been completed, and all the characters have been displayed, the cout statement will be given a line break on the terminal screen. Our last example code has been completed successfully and is ready to be compiled.

The code has been executed within the shell to display the result. In return, it displayed the original and converted string on the screen, as demonstrated below.

Conclusion:

To sum up, this was all about using the toupper() method in C++ in different ways. We have also applied this method to strings, integers, characters, and special symbols. The use of the “while” and “for” loop has been very much supportive of making it easy for users. Lastly, we have implemented four different examples on Ubuntu 20.04 operating system. However, you can use any other operating system as well.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.