How to Do Case-Insensitive Search for a Substring in C++? Last Updated : 21 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report A substring of a string is a contiguous sequence of characters within that string. In simpler terms, a substring is a part of a larger string that appears consecutively in it. In this article, we are going to learn how to do a case-insensitive search for a substring in C++. Example: Input:original = "Hello, World!"substring = "world"Output:Substring found in the original string.Case-Insensitive Search for a Substring in C++To do a case-insensitive search for a substring in C++, we can use the std::search() function with a custom comparator that converts the characters to be compared into lowercase using tolower() and then performs the comparison. Syntax of std::search()search(first1, first2, second1, second2, comp);here, first1: Iterator to the beginning of the first range.last1: Iterator to the last of the first range.first2: Iterator to the beginning of the second range.last1: Iterator to the last of the second range.comp: Custom comparator.This function will return the iterator to the first occurrence of the substring. If the substring is not found, then it will return the iterator to the end of the main string. C++ Program to to Do Case-Insensitive Search for a Substring C++ // C++ program to perform a case insensitive substring // search #include <algorithm> #include <iostream> #include <string> using namespace std; // Function to compare two characters in a case-insensitive // manner bool caseInsensitiveCharCompare(char a, char b) { // Convert both characters to lower case and compare // them return tolower(a) == tolower(b); } // Function to perform a case-insensitive search for a // substring in a string bool caseInsensitiveSubstringSearch(const string& str, const string& substr) { // Use the search function with the case-insensitive // character comparison function auto it = search(str.begin(), str.end(), substr.begin(), substr.end(), caseInsensitiveCharCompare); // Return true if the substring was found, false // otherwise return it != str.end(); } // Main function int main() { // Define the string and the substring string str = "Hello World"; string substr = "WORLD"; // Call the case-insensitive substring search function bool found = caseInsensitiveSubstringSearch(str, substr); // Print the result if (found) { cout << "Substring found" << endl; // If the substring was found } else { cout << "Substring not found" << endl; // If the substring was not found } return 0; } OutputSubstring found Time Complexity: O(N * M), where N is the size of the original string and M is the size of the substring to be searched.Space Complexity: O(1) Comment More infoAdvertise with us Next Article Find if a String ends With the Given Substring in C++ R rveerendra400 Follow Improve Article Tags : C++ Programs C++ cpp-string cpp-strings CPP Examples +1 More Practice Tags : CPPcpp-strings Similar Reads 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 How to Compare Two Substrings in a Character Array in C++? 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" Substrin 2 min read How to Match a Pattern in a String in C++? In C++, strings are sequences of characters stored in a char array. Matching a pattern in a string involves searching for a specific sequence of characters (the pattern) within a given string. In this article, we will learn how to match a pattern in a string in C++. Example: Input:Text: "GeeksForGee 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 Find if a String ends With the Given Substring in C++ You are given two strings. The task is to check if the main string ends with another string in C++. Example Input: mainString = "Hello! Geek"; targetString = "Geek" Output: Hello! Geek ends with Geek.We can find if the given string ends with the target string using the following method: Checking if 2 min read How to Find and Replace All Occurrences of a Substring in a C++ String? In C++, strings are sequences of characters that are used to represent textual data. In this article, we will learn how to find and replace all the occurrences of a particular substring in the given string. For Example, Input: str = "Lokesh is a good programmer, but Lokesh is still in the learning p 2 min read Like