Before checking the examples of capitalizing a string, we need to see the example of capitalizing a single character in C++. So, you need to start your first example with the generation of a new c++ file. This has been done within the shell terminal of the Ubuntu 20.04 system. SO, the “touch” instruction has been utilized so far. The new file will be opened in the GNU Nano editor utilizing a “nano” command.
The code has been started with some useful and needed header files, i.e., “iostream” and “cctype”. The namespace is mentioned in the code after the inclusion of header files. The main method has been started with the char type variable “var” that has been using the “toupper” built-in function to directly capitalize a character “x”. The cout statement is used here to display the capitalized character in the terminal. The program has been completed here.
The program has been saved using the “Ctrl+S” and quitted the nano editor with the “Ctrl+X”. It’s time to compile the c++ file first. For that, you need to have a “g++” compiler already been configured on your Ubuntu 20.04 system. So, we have been utilizing the g++ compiler so far. The “./a.out” is used here to execute the file after compilation. After this execution, we have got the character “x” in the capital form on our terminal screen.
Let’s have the first example of capitalizing a string in C++ using the “toupper()” built-in function. So, we have started our program with the inclusion of three main header files. One is “iostream” to input-output usage. The other is “cctype” and “cstring” to use the code’s character and string type variables. The namespace has been included after that. The main function has been started with the initialization of character type string array “A” having a value “Aqsa Yasin”. This string contains some lowercase characters and some capital letters.
Another character type variable, “ch” has been declared for use in the code further. The “for” loop has been used to iterate the string “A”. So, it’s been using the “toupper()” built-in function on each character of the string “A” to capitalize each. The cout statement is used to output each capitalized character in a loop. After the loop, the cout statement has given a line break. The code ends here and is saved using the “Ctrl+S”. Let’s quit the file using “Ctrl+X”.
The compilation got successful with the help of a g++ compiler command. The execution of this file with the “./a.out” command got tremendous, and it shows the whole converted string into capital form within the shell terminal screen. This is how we convert a lowercase string into the uppercase string with the help of a “toupper()” function in C++.
Let’s begin with another similar example of capitalizing a string in C++ using the “toupper” function. So, we have started our program with two header files, i.e., “iostream” and “string”. After using namespace, we have started our main function with the declaration of a string variable “s”. The cout statement asks a user to input some string in the shell. The getline function has been getting that input from the user using the “cin” statement and saving it to the variable “s”. The “for” loop has been iterating the string “s” to capitalize each character with the use of the “toupper()” function.
Each capitalized character is then saved into its exact location. This will be performed until the last character of a string “s”. After the “for” loop, we have utilized the “cout” statement to display the capitalized string “s” in the terminal in a single step without iterating it. The program is now completed here. Save it using the “Ctrl+S”.
The compilation and execution are productive, as seen in the image below. The user has added the “Linux tutorial” string. This string has been capitalized and displayed on the shell.
Let’s have another method to capitalize a string in C++. This method will utilize the “transform” function to convert all the strings into the capital form using some other built-in functions. So, we have started the code with header files and namespace. The main function has initialized a string variable “s” with a value “Aqsa Yasin”. The “transform()” function has been used here so far to capitalize the string “s”.
It’s been using the “begin()” and “end()” functions to show the start and end of a string to be capitalized. It illustrates that the string will be capitalized from its start to end. The next “s.begin()” saves the capitalized character from the start position. The “toupper” method has been used here with “::” to specify the “start” and “end” of a string “s”. The cout clause is here to display the capitalized string “s”.
The compilation is quite productive, and the execution shows us the capitalized string “AQSA YASIN” in the terminal.
Let’s have our last example of capitalizing a string in C++. Used the three required header files at the top of the file, i.e., “iostream”, “string”, and “algorithm”. The namespace is used with the “std” keyword in it. The main() function has been started with the declaration of a string “s”. The cout statement is used here to ask a user to add some input string. The getline() function utilizes the cin clause to get the input from the user and save it to the string “s”.
Then, we have used the same standard “transform()” function method here to convert all the lowercase characters of a string “s” to capital letters. We have used the “begin()” and “end()” functions within the transform method to do that. The “toupper()” function is essential in this capitalization method. Every character from start to the end of a string inputted by a user will be capitalized using the “toupper()” method on that string, and each character is saved again in its initial place. The cout statement is used here to display the whole capitalized string “s” in one step.
After compiling and running the file, we have been asked to enter a string. We have added a string in lowercase. The program has returned us to the string in uppercase, as shown below.
This article explains the different ways to capitalize a string in C++. We have used the simple transform function and “toupper” function for this purpose in our examples. All the examples are quite simple and understandable for every beginner and expert user. We believe this article proves to be the best of its version.