C++ string ignorecase: In the previous article, we have discussed aboutĀ Check if a String Starts with an Another given String. Let us learn Implementing a Case Insensitive String: in C++ Program.
The C++ string class internally stores characters in a char array, but all memory management, allocation, and null termination is handled by the string class itself, which is why it is simple to use. The length of a C++ string can be changed at runtime due to dynamic memory allocation, which is similar to vectors. Because the string class is a container class, we can iterate over all of its characters with an iterator, just like we can with vectors, sets, and maps, but we usually use a simple for loop to iterate over the characters and index them with the [] operator.
Examples:
Example 1:
Input:
string mainstring = "This is BTechGeeks" string matchstring = "this"
Output:
Given match string is found in mainstring
Example 2:
Input:
string mainstring = "This is BTechGeeks" string matchstring = "tis"
Output:
Given match string is not found in mainstring
String.find c++: In this article, we will look at how to use C++ to find a Case Insensitive Sub String in a given string.
Implementing a Case Insensitive String::find in C++
Method #1:Using std::find()
Convert both the mainstring and matchstring to lowercase.
Then use the std::string::find is used to search for substrings within a string.
Below is the implementation:
#include <bits/stdc++.h> using namespace std; int main() { // given main string string mainstring = "Hello This is BTechGeeks"; // given to match string string matchstring = "BTechGeeks"; // Converting mainstring to lower_case transform(mainstring.begin(), mainstring.end(), mainstring.begin(), ::tolower); // Converting matchstring to lower_case transform(matchstring.begin(), matchstring.end(), matchstring.begin(), ::tolower); // using find() function to find position int position = mainstring.find(matchstring, 0); // if the element is not found then position will be -1 if (position == -1) cout << "given matchstring is not found in " "mainstring" << endl; else cout << "given matchstring is found at position " << position << endl; }
Output:
given matchstring is found at position 14
Method #2:Using boost::icontains
We can use boost::icontains to see if a substring exists in a given string.
Below is the implementation:
#include <bits/stdc++.h> #include <boost/algorithm/string.hpp> using namespace std; int main() { // given main string string mainstring = "Hello This is BTechGeeks"; // given to match string string matchstring = "BTechGeeks"; // checking if the match string is present in main // string bool res = boost::icontains(mainstring, matchstring); if (res) cout << "Given match string is found in mainstring" << endl; else cout << "given matchstring is not found in " "mainstring" << endl; }
Output:
Given match string is found in mainstring
Related Programs:
- find all occurrences of a sub string in a string cpp both case sensitive insensitive
- convert first letter of each word of a string to upper case in cpp
- converting a string to upper lower case in cpp using stl boost library
- python check if a string contains a sub string case insensitive
- how to erase elements from a list in cpp using iterators
- cpp how to reverse a list or sub list in place
- cpp unordered_map erase elements while iterating in a loop