How to Check if a Substring Exists in a Char Array in C++? Last Updated : 08 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, Char array and std::string both are used to store a sequence of characters to represent textual data. In this article, we will learn how to check if a substring exists within a char array in C++. Example Input: charArray[]= "Hello, Geek" subString="Geek" Output: Geek substring is found in char array: Hello, Geek! Search for a Substring in Char ArrayThe C++ STL provides the std::find() function to search for a given substring in a range of values. If the substring is found, then find() function will return the index of the first occurrence of the given substring.If the substring is not found, then std::string::npos (no position) is returned which indicates that the given substring does not exist in a char array.C++ Program to Search for a Substring in Char Array C++ // C++ program to check if a substring is present in a char // array #include <iostream> #include <string> using namespace std; int main() { // character array char char_arr[] = "Hello, Geek!"; // substring const char* sub_string = "Geek"; // converting character array to string using string string char_arr_str = char_arr; // use the find method to check substring present or not if (char_arr_str.find(sub_string) != string::npos) { cout << sub_string << " substring is found in char array: " << char_arr_str << endl; } else { cout << sub_string << " substring is not found in char array: " << char_arr_str << endl; } return 0; } OutputGeek substring is found in char array: Hello, Geek! Time Complexity: O(N * M), where N is the size of the char array and M is the size of the substring.Auxiliary Space: O(1) We can also use strstr() function to check if a substring exists in a char array or not. Comment More infoAdvertise with us Next Article How to Check if a Substring Exists in a Char Array in C++? S sourabhcao9e0 Follow Improve Article Tags : C++ Programs C++ cpp-string CPP Examples Practice Tags : CPP Similar Reads How to Extract a Substring from a Character Array in C++? In C++, character arrays are used to store sequences of characters also known as strings. A substring is a part of a string that consists of a continuous sequence of characters from the string. In this article, we will learn how to extract a substring from a character array in C++. Extract a Substri 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 Check if a String is Empty in C++? In C++, strings are the sequence of characters that are stored as std::string class objects. In this article, we will learn how to check if a string is empty in C++ Example Input: str1="Hello! Geek" ; str2="" Output: str1 is not empty str2 is emptyChecking if the String is Empty in C++To check for a 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 How to Split a String into an Array in C++? In C++, splitting a string into an array of substrings means we have to parse the given string based on a delimiter and store each substring in an array. In this article, we will learn how to split a string into an array of substrings in C++. Example: Input: str= âHello, I am Geek from geeksforgeeks 2 min read Convert String to Char Array in C++ In C++, we usually represent text data using the std::string object. But in some cases, we may need to convert a std::string to a character array, the traditional C-style strings. In this article, we will learn how to convert the string to char array in C++.ExamplesInput: str = "geeksforgeeks"Output 4 min read C++ Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 4 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 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 Count unique substrings of a string S present in a wraparound string Given a string S which is an infinite wraparound string of the string "abcdefghijklmnopqrstuvwxyz", the task is to count the number of unique non-empty substrings of a string p are present in s. Examples: Input: S = "zab"Output: 6Explanation: All possible substrings are "z", "a", "b", "za", "ab", "z 12 min read Like