Open In App

How to Do Case-Insensitive Search for a Substring in C++?

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
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;
}


Output

Substring 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)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads