{"id":269,"date":"2015-11-23T08:09:49","date_gmt":"2015-11-23T08:09:49","guid":{"rendered":"http:\/\/www.csestack.org\/?p=269"},"modified":"2019-03-09T13:49:16","modified_gmt":"2019-03-09T08:19:16","slug":"implement-strstr-function-in-c","status":"publish","type":"post","link":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/","title":{"rendered":"How to Write a code to implement strstr function in C?"},"content":{"rendered":"<p>Before implementing strstr function, first\u00a0thing, you need to understand strstr function. What does strstr function\u00a0do? What are its uses?<\/p>\n<h4 style=\"text-align: center;\">What is strstr\u00a0function in C?<\/h4>\n<p>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\u00a0an inbuilt strstr function with its syntax.<\/p>\n<p class=\"note\"><strong>Syntax Declaration for strstr function: <\/strong><\/p>\n<pre class=\"note\"><code>int strstr(char*, char*);<\/code><\/pre>\n<p>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.<\/p>\n<p>If it founds substring in the base string, it returns 1. Otherwise, it returns 0.<\/p>\n<p>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 <code>strstr()<\/code> function.<\/p>\n<p>I have been asked this question in <a href=\"http:\/\/www.csestack.org\/ibm-placement-papers-for-isl-isdl-profile-set-1-on-campus\/\">IBM placement interview<\/a> on campus.<\/p>\n<h4 style=\"text-align: center;\">Code to implement strstr function in C<\/h4>\n<p>Complete C program to write your own strstr\u00a0function.<\/p>\n<p>[cpp]<br \/>\n#include&lt;stdio.h&gt;<\/p>\n<p>int fStrStr(char* str, char* strSub)<br \/>\n{<br \/>\n\tint i=0, j=0;<br \/>\n\tint nTemp = i;<br \/>\n\tint nStrLen = strlen(str);<br \/>\n\tint nStrSubLen = strlen(strSub);<br \/>\n\tfor(i=0; i&lt;nStrLen-nStrSubLen; i++)<br \/>\n\t{<br \/>\n\t\tnTemp = i;<br \/>\n\t\tfor(j=0; j&lt;nStrSubLen; j++)<br \/>\n\t\t{<\/p>\n<p>\t\t\tif(str[nTemp]==strSub[j])<br \/>\n\t\t\t{<br \/>\n\t\t\t\tif(j==nStrSubLen-1)<br \/>\n\t\t\t\t\treturn 1;<br \/>\n\t\t\t\tnTemp++;<br \/>\n\t\t\t}<br \/>\n\t\t\telse<br \/>\n\t\t\t\tbreak;<br \/>\n\t\t}<br \/>\n\t}<br \/>\n\treturn 0;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\n\tchar str[] = &quot;CSEStack&quot;;<br \/>\n\tchar strSub[] = &quot;SES&quot;;<\/p>\n<p>\tif(fStrStr(str, strSub))<br \/>\n\t\tprintf(&quot;Sub-string found.&quot;);<br \/>\n\telse<br \/>\n\t\tprintf(&quot;Sub-string not found.&quot;);<\/p>\n<p>}<\/p>\n<p>[\/cpp]<\/p>\n<p class=\"note\"><strong>Output:<\/strong><\/p>\n<p><strong>Case 1: <\/strong><\/p>\n<pre>char str[] = \"CSEStack\";\r\nchar strSub[] = \"SES\";\r\n=&gt;Sub-string found.<\/pre>\n<p><strong>Case 2: <\/strong><\/p>\n<pre>char str[] = \"CSEStack\";\r\nchar strSub[] = \"SDS\";\r\n=&gt;Sub-string not found.<\/pre>\n<p>This is one of the <a href=\"https:\/\/www.csestack.org\/interview-coding-questions-programming-language\/\">top\u00a0coding questions asked in an interview<\/a>.<\/p>\n<p><strong>Note:<\/strong> Here, I am using <code>fstrstr()<\/code> function that will work as an inbuilt function <code>strstr()<\/code>. Even you can use the same function name as <code>strstr()<\/code>. If there is inbuilt as well as user-defined function with the same name, the user-defined function will get the\u00a0call.<\/p>\n<p><strong>Time Complexity:\u00a0<\/strong><code>O(n^2)<\/code><\/p>\n<p>As in the worst case, we are comparing every letter in the base string with every letter in the substring, it&#8217;s complexity is <code>O(n^2)<\/code>.<\/p>\n<p><strong>Difficulty Level:<\/strong> Medium<\/p>\n<p>Do you have any other solution? Let&#8217;s code it in the comment section below.<\/p>\n<p style=\"text-align: center;\">Happy Programming!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a program to implement strstr function in C. Complete code explained about implementing string function strstr.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,7],"tags":[272],"class_list":["post-269","post","type-post","status-publish","format-standard","hentry","category-c-cpp","category-code","tag-c-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.3 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Write a code to implement strstr function in C?<\/title>\n<meta name=\"description\" content=\"Now here instead of using inbuilt strstr function, we want to implement strstr function in C. This function will work same like as inbuilt strstr.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Write a code to implement strstr function in C?\" \/>\n<meta property=\"og:description\" content=\"Now here instead of using inbuilt strstr function, we want to implement strstr function in C. This function will work same like as inbuilt strstr.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"CSEstack\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/aniruddha.ca\" \/>\n<meta property=\"article:published_time\" content=\"2015-11-23T08:09:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-03-09T08:19:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1280\" \/>\n\t<meta property=\"og:image:height\" content=\"720\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Aniruddha Chaudhari\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:site\" content=\"@ani_chaudhari\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Aniruddha Chaudhari\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/\"},\"author\":{\"name\":\"Aniruddha Chaudhari\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"headline\":\"How to Write a code to implement strstr function in C?\",\"datePublished\":\"2015-11-23T08:09:49+00:00\",\"dateModified\":\"2019-03-09T08:19:16+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/\"},\"wordCount\":384,\"commentCount\":18,\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"keywords\":[\"c-string\"],\"articleSection\":[\"C \\\/ C++\",\"Code\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/\",\"name\":\"How to Write a code to implement strstr function in C?\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\"},\"datePublished\":\"2015-11-23T08:09:49+00:00\",\"dateModified\":\"2019-03-09T08:19:16+00:00\",\"description\":\"Now here instead of using inbuilt strstr function, we want to implement strstr function in C. This function will work same like as inbuilt strstr.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/implement-strstr-function-in-c\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.csestack.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Write a code to implement strstr function in C?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#website\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/\",\"name\":\"CSEstack\",\"description\":\"Computer Science &amp; Programming Portal\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.csestack.org\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\\\/\\\/www.csestack.org\\\/#\\\/schema\\\/person\\\/634ef1a9c4f38b0d340c6d45fa771218\",\"name\":\"Aniruddha Chaudhari\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"url\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"contentUrl\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\",\"width\":634,\"height\":634,\"caption\":\"Aniruddha Chaudhari\"},\"logo\":{\"@id\":\"https:\\\/\\\/www.csestack.org\\\/wp-content\\\/uploads\\\/2019\\\/03\\\/Aniruddha-Chaudhari.jpg\"},\"description\":\"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.\",\"sameAs\":[\"https:\\\/\\\/www.csestack.org\",\"https:\\\/\\\/www.facebook.com\\\/aniruddha.ca\",\"pythonwithani\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/aniruddha28\\\/\",\"https:\\\/\\\/x.com\\\/ani_chaudhari\",\"https:\\\/\\\/www.youtube.com\\\/channel\\\/UCw0a__B0eJsvCujkSIfLTAA\"],\"url\":\"https:\\\/\\\/www.csestack.org\\\/author\\\/anicse\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Write a code to implement strstr function in C?","description":"Now here instead of using inbuilt strstr function, we want to implement strstr function in C. This function will work same like as inbuilt strstr.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/","og_locale":"en_US","og_type":"article","og_title":"How to Write a code to implement strstr function in C?","og_description":"Now here instead of using inbuilt strstr function, we want to implement strstr function in C. This function will work same like as inbuilt strstr.","og_url":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/","og_site_name":"CSEstack","article_publisher":"https:\/\/www.facebook.com\/aniruddha.ca","article_author":"https:\/\/www.facebook.com\/aniruddha.ca","article_published_time":"2015-11-23T08:09:49+00:00","article_modified_time":"2019-03-09T08:19:16+00:00","og_image":[{"width":1280,"height":720,"url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2024\/01\/csestack-blog.jpg","type":"image\/jpeg"}],"author":"Aniruddha Chaudhari","twitter_card":"summary_large_image","twitter_creator":"@ani_chaudhari","twitter_site":"@ani_chaudhari","twitter_misc":{"Written by":"Aniruddha Chaudhari","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/#article","isPartOf":{"@id":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/"},"author":{"name":"Aniruddha Chaudhari","@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"headline":"How to Write a code to implement strstr function in C?","datePublished":"2015-11-23T08:09:49+00:00","dateModified":"2019-03-09T08:19:16+00:00","mainEntityOfPage":{"@id":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/"},"wordCount":384,"commentCount":18,"publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"keywords":["c-string"],"articleSection":["C \/ C++","Code"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.csestack.org\/implement-strstr-function-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/","url":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/","name":"How to Write a code to implement strstr function in C?","isPartOf":{"@id":"https:\/\/www.csestack.org\/#website"},"datePublished":"2015-11-23T08:09:49+00:00","dateModified":"2019-03-09T08:19:16+00:00","description":"Now here instead of using inbuilt strstr function, we want to implement strstr function in C. This function will work same like as inbuilt strstr.","breadcrumb":{"@id":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.csestack.org\/implement-strstr-function-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.csestack.org\/implement-strstr-function-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.csestack.org\/"},{"@type":"ListItem","position":2,"name":"How to Write a code to implement strstr function in C?"}]},{"@type":"WebSite","@id":"https:\/\/www.csestack.org\/#website","url":"https:\/\/www.csestack.org\/","name":"CSEstack","description":"Computer Science &amp; Programming Portal","publisher":{"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.csestack.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.csestack.org\/#\/schema\/person\/634ef1a9c4f38b0d340c6d45fa771218","name":"Aniruddha Chaudhari","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","url":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","contentUrl":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg","width":634,"height":634,"caption":"Aniruddha Chaudhari"},"logo":{"@id":"https:\/\/www.csestack.org\/wp-content\/uploads\/2019\/03\/Aniruddha-Chaudhari.jpg"},"description":"I am a Python enthusiast who loves Linux and Vim. I hold a Master of Computer Science degree from NIT Trichy and have 10 years of experience in the IT industry, focusing on the Software Development Lifecycle from Requirements Gathering, Design, Development to Deployment. I have worked at IBM, Ericsson, and NetApp, and I share my knowledge on CSEstack.org.","sameAs":["https:\/\/www.csestack.org","https:\/\/www.facebook.com\/aniruddha.ca","pythonwithani","https:\/\/www.linkedin.com\/in\/aniruddha28\/","https:\/\/x.com\/ani_chaudhari","https:\/\/www.youtube.com\/channel\/UCw0a__B0eJsvCujkSIfLTAA"],"url":"https:\/\/www.csestack.org\/author\/anicse\/"}]}},"_links":{"self":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/269","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/comments?post=269"}],"version-history":[{"count":8,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/269\/revisions"}],"predecessor-version":[{"id":4731,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/posts\/269\/revisions\/4731"}],"wp:attachment":[{"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/media?parent=269"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/categories?post=269"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.csestack.org\/wp-json\/wp\/v2\/tags?post=269"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}