This tutorial will discuss about unique ways to check if char array contains a string in C++.
Table Of Contents
Technique 1: Check if a char array contains a substring
The strstr()
function takes two strings as arguments i.e.
strstr(const char* source, const char* target)
It looks for the first occurrence of target
string in the source
string, and returns the pointer to the first character of the matched string in the source
string. If the target
string does not exists in the source
string then it returns NULL.
We can use this strstr()
function to check if a char array contains a string or not. We have created a function for it,
// Check if a char array contains a substring bool contains(const char * mainStr, const char * subString) { bool result = false; // Get the pointer to first occurrence of // string "subStr" in the "mainStr" String const char * ptr = strstr(mainStr, subString) ; // If substring does not exits in the main string, // then ptr will be NULL if (ptr != NULL) { result = true; } return result; }
We can use this function to check if the char array contains the given substring or not.
Let’s see the complete example,
Frequently Asked:
#include <iostream> #include <string.h> // Check if a char array contains a substring bool contains(const char * mainStr, const char * subString) { bool result = false; // Get the pointer to first occurrence of // string "subStr" in the "mainStr" String const char * ptr = strstr(mainStr, subString) ; // If substring does not exits in the main string, // then ptr will be NULL if (ptr != NULL) { result = true; } return result; } int main() { char mainStr[50] = "This is a sample text"; char subString[50] = "sample"; // Check if Char Array contains a string if( contains(mainStr, subString) ) { std::cout<<"Char Array contains the string \n"; } else { std::cout<<"Char Array does not contains the string \n"; } return 0; }
Output :
Char Array contains the string
Technique 2: Check if a char array contains a substring
We can cast the char array to a std::string
object, and then use the string::find()
function to get the index position of first occurrence of another substring in the string. If given substring does not exists in the string, the the ‘find()’ function will return std::string::npos
.
We have created a separate function contains()
for this,
// Check if a char array contains a substring bool contains( const char * mainStr, const char * subString) { // Convert char array to string std::string strObj(mainStr) ; // Get the index position of substring in the string auto pos = strObj.find(subString) ; return pos != std::string::npos; }
We can use this function to check if the char array contains the given substring or not.
Let’s see the complete example,
Pointers in C/C++ [Full Course]
#include <iostream> #include <string> // Check if a char array contains a substring bool contains( const char * mainStr, const char * subString) { // Convert char array to string std::string strObj(mainStr) ; // Get the index position of substring in the string auto pos = strObj.find(subString) ; return pos != std::string::npos; } int main() { char mainStr[50] = "This is a sample text"; char subString[50] = "sample"; // Check if Char Array contains a string if(contains(mainStr, subString) ) { std::cout<<"Char Array contains the string \n"; } else { std::cout<<"Char Array does not contains the string \n"; } return 0; }
Output :
Char Array contains the string
Summary
Today we learned about several ways to check if char array contains a string in C++. Thanks.