Before implementing strstr function, first thing, you need to understand strstr function. What does strstr function do? What are its uses?
Strstr is the inbuilt string function given in C language. This function is used to find the substring in other string. Before to write our own code to implement the strstr function in C, I briefly introduce you an inbuilt strstr function with its syntax.
Syntax Declaration for strstr function:
int strstr(char*, char*);
This function is very easy to use in c language. It has two arguments as char strings. First string contents base string in which we want to search sub-string. The Second argument is sub-string that need to be searched in base string.
If it founds substring in the base string, it returns 1. Otherwise, it returns 0.
Now here, instead of using an inbuilt strstr function, we want to implement a strstr function in C. This function will work the same like as inbuilt strstr()
function.
I have been asked this question in IBM placement interview on campus.
Complete C program to write your own strstr function.
#include<stdio.h> int fStrStr(char* str, char* strSub) { int i=0, j=0; int nTemp = i; int nStrLen = strlen(str); int nStrSubLen = strlen(strSub); for(i=0; i<nStrLen-nStrSubLen; i++) { nTemp = i; for(j=0; j<nStrSubLen; j++) { if(str[nTemp]==strSub[j]) { if(j==nStrSubLen-1) return 1; nTemp++; } else break; } } return 0; } int main() { char str[] = "CSEStack"; char strSub[] = "SES"; if(fStrStr(str, strSub)) printf("Sub-string found."); else printf("Sub-string not found."); }
Output:
Case 1:
char str[] = "CSEStack"; char strSub[] = "SES"; =>Sub-string found.
Case 2:
char str[] = "CSEStack"; char strSub[] = "SDS"; =>Sub-string not found.
This is one of the top coding questions asked in an interview.
Note: Here, I am using fstrstr()
function that will work as an inbuilt function strstr()
. Even you can use the same function name as strstr()
. If there is inbuilt as well as user-defined function with the same name, the user-defined function will get the call.
Time Complexity: O(n^2)
As in the worst case, we are comparing every letter in the base string with every letter in the substring, it’s complexity is O(n^2)
.
Difficulty Level: Medium
Do you have any other solution? Let’s code it in the comment section below.
Happy Programming!
Hi ,i found your program to be wrong
I gave string as jeevan
Substring as euio
The output is
Sbstring found only ..
Please correct the mistake as soon as possible
Hi Jeevan,
I made the following changes as per your requirement
char str[] = “jeevan”;
char strSub[] = “euio”;
The output of the program is “Sub-string not found.”
Please check at your end at once.
HI ,i wanna also print the remaining substring how will i do?
Hi Amna,
Can you give an example? what do you want to print for the both cases of matching and nonmatching string?
When i do the following
Case 1:
char str[] = “apple”;
char strSub[] = “apple”;
The output of the program is “Sub-string not found.”
Case 2:
char str[] = “apple”;
char strSub[] = “apple”;
The output of the program is “Sub-string found.”
The program is not behaving properly when both the strings are exact match.
I tried running this code for exact match. I am getting correct answer. I will check for more times and if I get output as “Sub-string not found.”, will try to rectify the problem by putting some checkpoints.
Thanks for putting your point and observation.
If we give both strings are equal, due to this check “i<nStrLen-nStrSubLen" it is not entering into the logic and returning zero.
I did not get any issue with this program. It is running completely fine.
Thanks Andrew!
Hi, I truly appreciate your effort but I have a question.
How can you return 1 and then do increment? Increment will never happen as you have already returned from that function (Line 17-18).
Hey Hi,
There is explicit condition “if(j==nStrSubLen-1)” for “return 1”. If thay condtion goes true, we return 1. If this condtion is false, we are incrementing nTemp variable.
Hope this clear your doubt.
I always thought that
strstr
returned a pointer (char *) and not int.Yeah. It’s a bit confusing. I hope it is clear now.
Hello, how or what would I have to change if I wish the function to return a char* instead of int?
Here we are implementing the strstr function and it returns one if the substring found, otherwise zero. You can also return the pointer of the first occurrence of a substring in case the substring found.
Sir do you know Kali Linux because I have recently installed and tried to improve my hacking skill for educational purposes. I get lots of errors or even I can’t reach my objective, to be honest, I followed the steps shown on YouTube. I need an individual person to teach this OS. And I saw that you were offering free sessions. If I can, can I join your free session and gain knowledge from you? I am a 3rd-year college student in order to show my profile more valuable I need to learn this OS. Kindly help me with the same. I hope positive response from you sir.
Thank you
Hi Sabari. I’m glad to see your interest. Right now, as I’m occupied with my other priorities, I am not hosting any new sessions on Linux. But you can always ask your questions. Best wishes!