![]() |
VOOZH | about |
In C++, character arrays are used to store a sequence of characters also known as strings. A Substring is a continuous sequence of characters within a string. In this article, we will learn how we can compare two substrings in a character array in C++.
Examples:
Input: string: "Hello World" Substring1: "Hello" Substring 2: "World" Output: Substrings are not equal.
To compare two substrings in a character array in C++, we can use the std::substr() method to extract the substrings we want to compare from the character array. Then, we can compare the two substrings using the equality operator(==) .
The following program illustrates how we can compare two substrings in a character array in C++:
The substrings are equal.
Time Complexity: O(N + K) where N is the length of the character array and K is the average length of the substring.
Auxiliary Space: O(K)