Learn More String Functions in C++ Programming



In the previous two articles, we introduced strings. Then, we learned some of the useful functions that C++ offers to manipulate strings. In this article, we are going to continue with the string functions. Have a nice reading!

The strrev() Function
This is the string reverse function. It reverses a given string by reversing the order of all characters. As with other string functions, the string.h library file must be included.

Let’s see how this function works by using an example.

When executed, the program should give the following output:


Notice that the original string has been reversed; i.e. the string variable greeting has its string reversed.

strlwr() and strupr() Functions
The strlwr() function converts all the letters in a string to lowercase letters. Conversely, the strupr() function converts all to uppercase. Both functions change the original string variable, and both require including the string.h header file.

The following program converts the letters of the name of my favorite Italian footballer “Alessandro Del Piero” into lowercase, then uppercase, and prints both forms.

The program should print the following output:



The strset() Function
This function sets all characters in a string to a given character.



STRINGVAR is a string variable.
CH is the character to set all characters in STRINGVAR to.

The following program sets all characters in the string str to ‘=’

Executing this code should print the following output:


The strstr() Function
This function searches a string for the first occurrence of another sub-string. It returns a pointer (for now, consider it as an address) to the first occurrence of the sub-string , or null if the sub-string is not found.

The following program searches for the first occurrence of the string “ro” within the name of the Italian superstar “Alessandro Del Piero”.

When executed, the program should print the part of the superstring where the sub-string “ro” was first matched.


One thing you will find different in this program is the declaration of the main() function.

The main() function is like any function; it can take arguments, and return values. In all the previous programs we have written so far in this series, we have always specified void as the return type of the main(), i.e. it doesn’t return anything. In this program, we have specified int as the type of the returned value, so the function has to return an integer number. The main() function will return this integer to the program or the operating system that invoked it. This is very useful to report back the exit status of the main(); i.e. the program started by the main() function. If you are a UNIX/Linux user, you would know about the exit status of commands and scripts.

The string Class
Fortunately, C++ was nice enough to introduce new standard class to implement strings. The string class handles all the tasks of sizing and memory management on behalf for you! In addition, it offers a set of useful functions and operators that facilitate many of your needs. Isn’t it great?

The string class is considered as a lately-developed feature (which was introduced starting with C++ 98 in 1998). As a result, the old C++ compilers don’t support this class. For this reason, we are going to install another new IDE for developing, compiling and executing C++ programs. OK, let’s do it!

Installing Dev-C++
Dev-C++ is free IDE software for developing C and C++ programs. To download Dev-C++, open your Internet browser, and navigate to the following URL: http://sourceforge.net/projects/orwelldevcpp/


Click the green button “Download Dev-Cpp …… Setup.exe” to download the setup file. When the download is finished, double-click the setup file, and follow the installation wizard to complete the installation. When complete, you will find a new shortcut on your desktop:



In this article, we have continued with the String functions.

  • The strrev() reverses a string.
  • The strlwr() converts all letters in a string to lowercase.
  • The strupr() converts all letters in a string to uppercase.
  • The strset() sets all characters in a string to a given character.
  • The strstr() finds the first occurrence of a substring in a superstring.
  • The standard string class was introduced in 1998, and has many useful features.

In the next article, we are going to learn and use the string class. An interesting article worth waiting for. So, see you there!


Please enter your comment!
Please enter your name here