Consider a string s ="PROGRAMMING", then all the continuous parts of any length like "PRO", "GRAM", "RAM", and so on are called the substrings of a string s.
So, to extract these substrings from a specific string we have a pre-defined function called substr() that exists in the header file in C++ for handling all types of string operations like strcat(), append() etc.
In this article, we will go in-depth about extracting substrings from strings, syntax, parameters, applications, and programs in C++ with complete code.
What is a substring in C++?
A continuous part of a string is known as its sub-string. A string can have many substrings of various lengths. Like in set theory, a set can have multiple subsets, similarly, a string can contain many sub-strings of different lengths.
In C++, substr() is a pre-defined function used to extract a sub-string of a given length from a specific string. To access the substr() function in our C++ program, we need to include a header file called for string handling.
The substr() function takes 2 parameters pos and len as arguments and a newly constructed string object with its value initialized to a copy of a sub-string of this object is returned.
Here, the pos parameter defines the index or location from where we have to start copying the substring elements and len defines the length of that substring or the extent to which we have to copy the elements of a string.
Syntax of substr() function
Here is the official syntax for using substr() function in C++:
Parameters of substr() function
There are 3 parameters:
- pos: Position/index of the first character to be copied or the location from where we have to originate our substring.
- len: Length of the sub-string we need to extract after the pos location/index.
- size_t: It is an unsigned integral type.
substr() It returns a string object. It generates a new string with its value initialized to a copy of a sub-string extracted from the original string.
C++ Program of substr() function
The following C++ program demonstrates how to find a substring in a string:
Important Applications of substr() function
There are many interesting applications of it in the coding world:
01) Extract a sub-string after a given character
In this, we have been given a specific character that will exist in our original string, we have to generate a sub-string after that character only.
Example: Extract everything after the “:” in the string “Topic:sub-string“. The following is the C++ program for the above:
02) Extract a sub-string before a given character
In this, contrary to the above, we have been given a specific character that will exist in our original string, we have to generate a sub-string before that character only.
Example: Extract everything before the “:” in the string “Topic:sub-string“. The following is the program:
03) Find all the sub-strings from a given string
In this, we have to extract all the possible sub-strings from a given sub-string. The following is the C++ program for the above :
04) Sum of all the sub-strings as numbers
An integer will be given in the string format and we need to find the sum of all the possible sub-strings that can be extracted from the given string. Here is the code:
05) Pattern Matching using sub-string
In this, we need to find whether the given string is present or not in the original string and if present then at what location/index. Here is the code:
So, now you know everything about the substring function is used to extract all the possible sub-strings of various lengths from a given string. We also provided a program on how to find substring in a string in C++.
Congratulations on getting this far! Now give yourself a pat on the back. Good job!