Implementing a Case Insensitive string::find in C++

In this article we will discuss how to find a Case Insensitive Sub String in a given string in C++ using,

  • STL
  • Boost Library

std::string provides a method std::string::find to search for the sub string inside a given string, but this function is case sensitive i.e.

std::string data = "Hi this is a sample string";
size_t pos = data.find("SAMPLE");

In above code, value of pos will be std::string::npos i.e.
pos == std::string::npos

Because, std::string::find is case sensitive and can not find ‘SAMPLE’ in the given string.  Now let’s implement a case sensitive version of std::string::find

Finding A Case Insensitive Sub String in C++ using STL

Logic:

Create a new find function that will convert both the given string and sub string to lower case and then use the std::string::find to search sub string in the string i.e.

/*
 * Find Case Insensitive Sub String in a given substring
 */
size_t findCaseInsensitive(std::string data, std::string toSearch, size_t pos = 0)
{
	// Convert complete given String to lower case
	std::transform(data.begin(), data.end(), data.begin(), ::tolower);
	// Convert complete given Sub String to lower case
	std::transform(toSearch.begin(), toSearch.end(), toSearch.begin(), ::tolower);
	// Find sub string in given string
	return data.find(toSearch, pos);
}

Let’s see how to use this function,
std::string data = "Hi this is a sample string";
size_t pos = findCaseInsensitive(data, "SAMPLE");
if( pos != std::string::npos)
	std::cout<<"'SAMPLE' Found at "<<pos<<std::endl;
else
	std::cout<<"'SAMPLE' Not Found"<<std::endl;

Unlike std::string::find, here we are able to find “SAMPLE” in “Hi this is a sample string”

Advertisements

Complete code example is as follows,

#include <iostream>
#include <string>
#include <algorithm>
/*
 * Find Case Insensitive Sub String in a given substring
 */
size_t findCaseInsensitive(std::string data, std::string toSearch, size_t pos = 0)
{
	// Convert complete given String to lower case
	std::transform(data.begin(), data.end(), data.begin(), ::tolower);
	// Convert complete given Sub String to lower case
	std::transform(toSearch.begin(), toSearch.end(), toSearch.begin(), ::tolower);
	// Find sub string in given string
	return data.find(toSearch, pos);
}

int main()
{
	std::string data = "Hi this is a sample string";

	// Using std::string::find to find a Sub string in given string
	size_t pos = data.find("SAMPLE");

	if( pos != std::string::npos)
		std::cout<<"'SAMPLE' Found at "<<pos<<std::endl;
	else
		std::cout<<"'SAMPLE' Not Found"<<std::endl;

	// Using Customized find function to find a Sub string in given string
	pos = findCaseInsensitive(data, "SAMPLE");

	if( pos != std::string::npos)
		std::cout<<"'SAMPLE' Found at "<<pos<<std::endl;
	else
		std::cout<<"'SAMPLE' Not Found"<<std::endl;

	return 0;
}

Output
'SAMPLE' Not Found
'SAMPLE' Found at 13

Finding A Case Insensitive Sub String using Boost::icontains

With boost::icontains we can check if a sub string exists in a given string i.e.

std::string data = "Hi this is a sample string";

// Case Insensitive Sub String Search
bool result = boost::icontains(data, "SAMPLE");

But we can not find out the position of substring with it 🙁

Complete code example is as follows,

#include <iostream>
#include <string>
#include <boost/algorithm/string.hpp>



int main()
{
	std::string data = "Hi this is a sample string";

	// Case Insensitive Sub String Search
	bool result = boost::icontains(data, "SAMPLE");

	if(result)
		std::cout<<"'SAMPLE' is Present in ---"<<data<<std::endl;
	else
		std::cout<<"'SAMPLE' is Not Present in ---"<<data<<std::endl;


}

Output
'SAMPLE' is Present in ---Hi this is a sample string

 

Do you want to Learn Modern C++ from best?

We have curated a list of Best C++ Courses, that will teach you the cutting edge Modern C++ from the absolute beginning to advanced level. It will also introduce to you the word of Smart Pointers, Move semantics, Rvalue, Lambda function, auto, Variadic template, range based for loops, Multi-threading and many other latest features of C++ i.e. from C++11 to C++20.

Check Detailed Reviews of Best Modern C++ Courses

Remember, C++ requires a lot of patience, persistence, and practice. So, start learning today.

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top