C++ Program To Check If A String Is Substring Of Another Last Updated : 20 Jan, 2022 Comments Improve Suggest changes Like Article Like Report 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: -1. Explanation: There is no occurrence of "practice" in "geeksforgeeks" Simple Approach: The idea is to run a loop from start to end and for every index in the given string check whether the sub-string can be formed from that index. This can be done by running a nested loop traversing the given string and in that loop run another loop checking for sub-string from every index. For example, consider there to be a string of length N and a substring of length M. Then run a nested loop, where the outer loop runs from 0 to (N-M) and the inner loop from 0 to M. For very index check if the sub-string traversed by the inner loop is the given sub-string or not. C++ // C++ program to check if a string is // substring of other. #include <bits/stdc++.h> using namespace std; // Returns true if s1 is substring // of s2 int isSubstring(string s1, string s2) { int M = s1.length(); int N = s2.length(); /* A loop to slide pat[] one by one */ for (int i = 0; i <= N - M; i++) { int j; /* For current index i, check for pattern match */ for (j = 0; j < M; j++) if (s2[i + j] != s1[j]) break; if (j == M) return i; } return -1; } // Driver code int main() { string s1 = "for"; string s2 = "geeksforgeeks"; int res = isSubstring(s1, s2); if (res == -1) cout << "Not present"; else cout << "Present at index " << res; return 0; } Output:Present at index 5 Complexity Analysis: Time complexity: O(m * n) where m and n are lengths of s1 and s2 respectively. A nested loop is used the outer loop runs from 0 to N-M and inner loop from 0 to M so the complexity is O(m*n).Space Complexity: O(1). As no extra space is required. An efficient solution is to use a O(n) searching algorithm like KMP algorithm, Z algorithm, etc.Language implementations: Java Substringsubstr in C++Python find Another Efficient Solution: An efficient solution would need only one traversal i.e. O(n) on the longer string s1. Here we will start traversing the string s1 and maintain a pointer for string s2 from 0th index.For each iteration we compare the current character in s1 and check it with the pointer at s2.If they match we increment the pointer on s2 by 1. And for every mismatch we set the pointer back to 0.Also keep a check when the s2 pointer value is equal to the length of string s2, if true we break and return the value (pointer of string s1 - pointer of string s2)Works with strings containing duplicate characters. C++ // C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; int Substr(string s2, string s1) { // pointing s2 int counter = 0; int i = 0; for(; i < s1.length(); i++) { if(counter==s2.length()) break; if(s2[counter]==s1[i]) { counter++; } else { // Special case where character preceding // the i'th character is duplicate if(counter > 0) { i -= counter; } counter = 0; } } return (counter < s2.length() ? -1 : i - counter); } // Driver code int main() { string s1 = "geeksfffffoorrfoorforgeeks"; cout << Substr("for", s1); return 0; } // This code is contributed by Manu Pathria Output:18 Complexity Analysis: The complexity of the above code will be still O(n*m) in the worst case and the space complexity is O(1). Please refer complete article on Check if a string is substring of another for more details! Comment More infoAdvertise with us Next Article C++ Program To Check If A String Is Substring Of Another kartik Follow Improve Article Tags : Strings Pattern Searching C++ Programs DSA Practice Tags : Pattern SearchingStrings Similar Reads Check if a string is suffix of another Given two strings s1 and s2, check if s1 is a suffix of s2. Or in simple words, we need to find whether string s2 ends with string s1. Examples : Input : s1 = "geeks" and s2 = "geeksforgeeks" Output : Yes Input : s1 = "world", s2 = "my first code is hello world" Output : Yes Input : s1 = "geeks" and 6 min read C++ Program to check if strings are rotations of each other or not Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false) Algorithm: areRotations(str1, str2) 1. Create a temp string and store concatenation of str1 to str1 in temp. temp = 2 min read C++ Program to Check if strings are rotations of each other or not | Set 2 Given two strings s1 and s2, check whether s2 is a rotation of s1. Examples: Input : ABACD, CDABA Output : True Input : GEEKS, EKSGE Output : True We have discussed an approach in earlier post which handles substring match as a pattern. In this post, we will be going to use KMP algorithm's lps (lon 2 min read How to Check if a Substring Exists in a Char Array in C++? 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 ch 2 min read C++ Program to Check String is Containing Only Digits Prerequisite: Strings in C++ The string is the collection of characters or text in a string variable, surrounded by double quotes. One question arises How can we check string contains only digits in C++? So, to solve this query we can use the methods mentioned in the article given below: Example: "1 3 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 Frequency of a substring in a string | Set 2 Given a string str of length N and a substring pattern of length M, the task is to find the frequency of occurrences of pattern as a substring in the given string. If pattern is present in the string str, then print "Yes" with the count of its occurrence. Otherwise, print "No". Examples: Input: str 6 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 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 C++ Program To Find Length Of The Longest Substring Without Repeating Characters Given a string str, find the length of the longest substring without repeating characters. For âABDEFGABEFâ, the longest substring are âBDEFGAâ and "DEFGAB", with length 6.For âBBBBâ the longest substring is âBâ, with length 1.For "GEEKSFORGEEKS", there are two longest substrings shown in the below 6 min read Like