How to Compare Two Substrings in a Character Array in C++? Last Updated : 22 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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.Compare Two Substrings in a Character Array in C++ 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(==) . C++ Program to Compare two Substrings in a Character Array The following program illustrates how we can compare two substrings in a character array in C++: C++ // C++ Program to illustrate how we can compare two // substrings in a character array #include <iostream> #include <string> using namespace std; int main() { // Initializing a character array char charArray[] = "GeeksforGeeks"; // Extracting substrings from the character array string substring1 = string(charArray).substr(0, 5); string substring2 = string(charArray).substr(8, 12); // Compare the substrings if (substring1 == substring2) { cout << "The substrings are equal." << endl; } else { cout << "The substrings are not equal." << endl; } return 0; } OutputThe 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) Comment More infoAdvertise with us Next Article How to Compare Two Substrings in a Character Array in C++? A abhay94517 Follow Improve Article Tags : C++ Programs C++ cpp-string cpp-array substring CPP Examples +2 More Practice Tags : CPP Similar Reads How to Extract a Substring from a Character Array in C++? In C++, character arrays are used to store sequences of characters also known as strings. A substring is a part of a string that consists of a continuous sequence of characters from the string. In this article, we will learn how to extract a substring from a character array in C++. Extract a Substri 2 min read How to Check if a Substring Exists in a Char Array in C++? In C++, Char array and std::string both are used to store a sequence of characters to represent textual data. In this article, we will learn how to check if a substring exists within a char array in C++. Example Input: charArray[]= "Hello, Geek" subString="Geek" Output: Geek substring is found in ch 2 min read How to Find the Length of a Substring in a char Array? 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: 5Finding Length o 2 min read How to Compare Arrays in C++? In C++, arrays are linear data structures that can store data of the same type in contiguous memory locations. In this article, we will learn how to compare two arrays to check whether they are equal or not in C++. Example: Input: arr1[] = {1, 2, 4, 3, 5, 11} arr2[] = {1 2, 3, 4 ,5} Output: arr1 and 2 min read How to Sort an Array of Strings Using Pointers in C++? In C++, sorting an array of strings using pointers is quite different from normal sorting because here the manipulation of pointers is done directly, and then according to which string is pointed by the pointer the sorting is done. The task is to sort a given array of strings using pointers. Example 2 min read C++ Program to Sort String of Characters Sorting a string means rearranging the characters of the given string in some defined order such as alphabetical order. In this article, we will learn how to sort a string by characters in C++.ExamplesInput: str = "geeksforgeeks"Output: "eeeefggkkorss"Explanation: The characters in the string are so 4 min read C++ Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 4 min read C++ Program to compare two string using pointers Given two strings, compare the strings using pointers Examples: Input: str1 = geeks, str2 = geeks Output: Both are equal Input: str1 = hello, str2 = hellu Output: Both are not equal As their length are same but characters are different The idea is to dereference given pointers, compare values and ad 1 min read Case-Insensitive String Comparison in C++ In C++, strings are sequences of characters that are used to store text data which can be in both uppercase and lowercase format. In this article, we will learn how we can compare two case-insensitive strings in C++. Example: Input: string1 = "WELCOME TO GEEKS FOR GEEKS"string2 = "welcome to geeks f 2 min read C++ Program To Check Whether Two Strings Are Anagram Of Each Other Write a function to check whether two given strings are anagram of each other or not. An anagram of a string is another string that contains the same characters, only the order of characters can be different. For example, "abcd" and "dabc" are an anagram of each other. We strongly recommend that you 7 min read Like