Creating Custom Utility Header Files in C++

Vaidhyanathan S M
TheLeanProgrammer
Published in
4 min readMay 9, 2021

When coding in C++, we make use of many built-in libraries to simplify our tasks. Sometimes, we do feel some functionalities to be missing in these libraries. What if we could create our own Custom Utility Header Files to add more functionality to the existing ones. We are going to learn to do the same in this tutorial. So, gear up! Let’s begin.

Introduction

Consider the string header file in C++. It almost has all the functions that we need to perform various string operations and manipulations. But we can add more useful functions. We feel the need for more addition of new functionalities in the libraries in situations such as Implementing a project, Solving Data Structures and Algorithms based problems and etc. We can take inspiration from other programming languages such as Python and Java and try to implement functions that you find useful.

Utility Header Files in C++

Implementation

Let us first create a file named String.h and declare the function prototypes as shown below.

Let us define the functions given as prototypes above one by one. The contains() function takes two strings as arguments and returns true if the second string is contained in the first string and false otherwise.

We keep track of the frequencies of the characters of the string s1 and s2 using maps mp1 and mp2. We compare the frequency while traversing through the map mp2 and if s2 is not contained in s1 then we return false and otherwise true.

Let’s move on to the next ones, i.e., startswith() and endswith(). Programmers familiar with Python would immediately understand what these functions do. These functions check whether the string starts with or ends with the specified sub-string. These functions take the main string, the specified sub-string, the starting and ending index as arguments, where the start and end index are optional arguments.

Now, the next function is capitalize(). This function takes string reference as an argument and capitalizes the first character of the string.

The next one is count(). This function takes two strings as arguments and returns the number of times string s2 appears in string s1.

Next, we are going to use function overloading to implement a function that searches for the first occurrence of the string s2 or char ch in the string s1. They are defined as follows.

Again, we are going to use function overloading to implement a function that searches for the last occurrence of the string s2 or char ch in the string s1. They are defined as follows.

The next set of functions return true if all the characters in the string satisfy a certain condition such as all are alphabets, all are lower-case letters, etc. The functions are defined as shown below.

The next function is istitle(). The function returns true if the string follows the rule of a title, i.e., if all the words in the string start with a capital letter. The function is defined as follows.

The next set of functions convert the given string to lowercase or uppercase. The string reference is passed as arguments to these functions. They are defined as shown below.

The next one is the split() function that takes the given string and the specified separator as arguments and returns a vector of strings split based on the separator.

The next function is swapcase() that takes the reference to the given string as an argument and changes the lowercase characters to uppercase characters and vice-versa.

The next one is title(). It takes the reference to the given string as an argument and converts the first character of each word in the string to uppercase.

Now, we are going to use function overloading to implement a function that replaces the specified value in the string with the specified value passed as an argument to the function. They are defined as follows.

We come to the end of this tutorial. The list of functions given here is not exhaustive and you can add your own functions that would make it more useful. If you are interested you might even want to contribute to the GitHub Repository that focuses on the very title of this article. The link to the same is here.

Hope you enjoyed reading the article!

If you have any queries please post in the comment section below. Connect with me on LinkedIn. Also, if you want to look at my amazing collection of apps developed, don’t forget to check Google Play Store.

Know more about me here.

With that being said, thanks for reading my article, and Happy Coding!

Don’t forget to follow The Lean Programmer Publication for more such articles, and subscribe to our newsletter tinyletter.com/TheLeanProgrammer

--

--

Vaidhyanathan S M
TheLeanProgrammer

Systems Engineer @TCS | Native Android Developer | Enthusiastic Programmer | Skilled in Python, C/C++, Java, Flutter and Flask.