![]() |
VOOZH | about |
In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++.
Example
Input:
str[] = "Hello, World!";
substr= "World";
Output: Length of substring World is: 5
To find the length of a substring in a char array iterate through the character array and use the std::strstr() function to search for the first occurrence of a substring within a character array if the substring is found calculate its length using the std::string::length() function.
The below example demonstrates how we can find the length of the given substring in a char array.
Length of the substring 'World': 5
Time Complexity: O(N), here N is the length of parent string.
Auxilliary Space: O(1)