{"id":26584,"date":"2017-12-20T21:46:03","date_gmt":"2017-12-20T16:16:03","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26584"},"modified":"2017-12-20T21:46:03","modified_gmt":"2017-12-20T16:16:03","slug":"substring-in-c-programming","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/substring-in-c-programming\/","title":{"rendered":"Substring in c programming"},"content":{"rendered":"<p>Substring in c programming: c programming code to find a substring from a given string and for all substrings of a string, For example substrings of string \u201cthe\u201d are \u201ct\u201d, \u201cth\u201d, \u201cthe\u201d, \u201ch\u201d, \u201che\u201d and \u201ce\u201d to find substring we create our own c substring function which returns a pointer to string. String address, length of substring required and position from where to extract substring are the three arguments passed to function. String.h does not contain any library function to directly find substring.<\/p>\n<h2 id=\"c-substring-code\">C substring code<\/h2>\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%20%0Aint%20main()%20%0A%7B%0A%20%20%20char%20string%5B1000%5D%2C%20sub%5B1000%5D%3B%0A%20%20%20int%20position%2C%20length%2C%20c%20%3D%200%3B%0A%20%0A%20%20%20printf(%22Input%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20printf(%22Enter%20the%20position%20and%20length%20of%20substring%5Cn%22)%3B%0A%20%20%20scanf(%22%25d%25d%22%2C%20%26position%2C%20%26length)%3B%0A%20%0A%20%20%20while%20(c%20%3C%20length)%20%7B%0A%20%20%20%20%20%20sub%5Bc%5D%20%3D%20string%5Bposition%2Bc-1%5D%3B%0A%20%20%20%20%20%20c%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20sub%5Bc%5D%20%3D%20\u2019%5C0\u2019%3B%0A%20%0A%20%20%20printf(%22Required%20substring%20is%20%5C%22%25s%5C%22%5Cn%22%2C%20sub)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<p><img fetchpriority=\"high\" decoding=\"async\" class=\"aligncenter size-full wp-image-26586\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/substring-c.png\" alt=\"Substring in c programming\" width=\"405\" height=\"157\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/substring-c.png 405w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/substring-c-300x116.png 300w\" sizes=\"(max-width: 405px) 100vw, 405px\" \/><\/p>\n<p>We create a function and pass it four arguments original string array, substring array, position and length of desired substring. As call by reference is used we do not need to return substring array. See another code below in which we return pointer to substring, which we create in our array using dynamic memory allocation.<\/p>\n[ad type=\u201dbanner\u201d]\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%20%0Avoid%20substring(char%20%5B%5D%2C%20char%5B%5D%2C%20int%2C%20int)%3B%0A%20%0Aint%20main()%20%0A%7B%0A%20%20%20char%20string%5B1000%5D%2C%20sub%5B1000%5D%3B%0A%20%20%20int%20position%2C%20length%2C%20c%20%3D%200%3B%0A%20%0A%20%20%20printf(%22Input%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20printf(%22Enter%20the%20position%20and%20length%20of%20substring%5Cn%22)%3B%0A%20%20%20scanf(%22%25d%25d%22%2C%20%26position%2C%20%26length)%3B%0A%20%0A%20%20%20substring(string%2C%20sub%2C%20position%2C%20length)%3B%0A%20%0A%20%20%20printf(%22Required%20substring%20is%20%5C%22%25s%5C%22%5Cn%22%2C%20sub)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0Avoid%20substring(char%20s%5B%5D%2C%20char%20sub%5B%5D%2C%20int%20p%2C%20int%20l)%20%7B%0A%20%20%20int%20c%20%3D%200%3B%0A%20%0A%20%20%20while%20(c%20%3C%20l)%20%7B%0A%20%20%20%20%20%20sub%5Bc%5D%20%3D%20s%5Bp%2Bc-1%5D%3B%0A%20%20%20%20%20%20c%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20sub%5Bc%5D%20%3D%20\u2019%5C0\u2019%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<h2 id=\"c-substring-program-using-pointers\">C substring program using pointers<\/h2>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstdlib.h%3E%0A%20%0Achar*%20substring(char*%2C%20int%2C%20int)%3B%0A%20%0Aint%20main()%20%0A%7B%0A%20%20%20char%20string%5B100%5D%2C%20*pointer%3B%0A%20%20%20int%20position%2C%20length%3B%0A%20%0A%20%20%20printf(%22Input%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20printf(%22Enter%20the%20position%20and%20length%20of%20substring%5Cn%22)%3B%0A%20%20%20scanf(%22%25d%25d%22%2C%26position%2C%20%26length)%3B%0A%20%0A%20%20%20pointer%20%3D%20substring(%20string%2C%20position%2C%20length)%3B%0A%20%0A%20%20%20printf(%22Required%20substring%20is%20%5C%22%25s%5C%22%5Cn%22%2C%20pointer)%3B%0A%20%0A%20%20%20free(pointer)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0A%2F*C%20substring%20function%3A%20It%20returns%20a%20pointer%20to%20the%20substring%20*%2F%0A%20%0Achar%20*substring(char%20*string%2C%20int%20position%2C%20int%20length)%20%0A%7B%0A%20%20%20char%20*pointer%3B%0A%20%20%20int%20c%3B%0A%20%0A%20%20%20pointer%20%3D%20malloc(length%2B1)%3B%0A%20%0A%20%20%20if%20(pointer%20%3D%3D%20NULL)%0A%20%20%20%7B%0A%20%20%20%20%20%20printf(%22Unable%20to%20allocate%20memory.%5Cn%22)%3B%0A%20%20%20%20%20%20exit(1)%3B%0A%20%20%20%7D%0A%20%0A%20%20%20for%20(c%20%3D%200%20%3B%20c%20%3C%20length%20%3B%20c%2B%2B)%0A%20%20%20%7B%0A%20%20%20%20%20%20*(pointer%2Bc)%20%3D%20*(string%2Bposition-1)%3B%20%20%20%20%20%20%0A%20%20%20%20%20%20string%2B%2B%3B%20%20%20%0A%20%20%20%7D%0A%20%0A%20%20%20*(pointer%2Bc)%20%3D%20\u2019%5C0\u2019%3B%0A%20%0A%20%20%20return%20pointer%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<h2 id=\"all-substrings-of-string\"><span id=\"c-code-for-all-substrings-of-a-string\">C code for all substrings of a string<\/span><\/h2>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%23include%20%3Cmalloc.h%3E%0A%20%0Achar*%20substring(char*%2C%20int%2C%20int)%3B%0A%20%0Aint%20main()%20%0A%7B%0A%20%20%20char%20string%5B100%5D%2C%20*pointer%3B%0A%20%20%20int%20position%20%3D%201%2C%20length%20%3D%201%2C%20temp%2C%20string_length%3B%0A%20%0A%20%20%20printf(%22Enter%20a%20string%5Cn%22)%3B%0A%20%20%20gets(string)%3B%0A%20%0A%20%20%20temp%20%3D%20string_length%20%3D%20strlen(string)%3B%0A%20%0A%20%20%20printf(%22Substring%20of%20%5C%22%25s%5C%22%20are%5Cn%22%2C%20string)%3B%0A%20%0A%20%20%20while%20(position%20%3C%3D%20string_length)%0A%20%20%20%7B%0A%20%20%20%20%20%20while%20(length%20%3C%3D%20temp)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20pointer%20%3D%20substring(string%2C%20position%2C%20length)%3B%0A%20%20%20%20%20%20%20%20%20printf(%22%25s%5Cn%22%2C%20pointer)%3B%0A%20%20%20%20%20%20%20%20%20free(pointer)%3B%0A%20%20%20%20%20%20%20%20%20length%2B%2B%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20temp\u2013%3B%0A%20%20%20%20%20%20position%2B%2B%3B%0A%20%20%20%20%20%20length%20%3D%201%3B%0A%20%20%20%7D%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20\u2033 message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-26585\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/all-substrings-c.png\" alt=\"Substring in c programming\" width=\"405\" height=\"255\" srcset=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/all-substrings-c.png 405w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/all-substrings-c-300x189.png 300w, https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/all-substrings-c-320x200.png 320w\" sizes=\"(max-width: 405px) 100vw, 405px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Substring in c programming &#8211; C Programming &#8211; c programming code to find a substring from a given string and for all substrings of a string.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69866,1],"tags":[72833,73202,79199,79202,79205,7378,72827,73163,73158,73164,74701,79210,79209,79206,79204,79201,79208,74702,79203,73157,73201,79196,74704,79200,73160,79207],"class_list":["post-26584","post","type-post","status-publish","format-standard","hentry","category-c-programming","category-coding","tag-c-array-of-strings","tag-c-compare-strings","tag-c-program-to-find-substring-in-a-string","tag-c-program-to-find-substring-in-a-string-using-pointers","tag-c-program-to-find-substring-of-string-using-library-function","tag-c-string","tag-c-string-array","tag-c-string-compare","tag-c-string-functions","tag-c-string-h","tag-c-string-to-int","tag-char-to-string","tag-control-string-in-c-programming","tag-extract-substring-from-string-in-c","tag-how-to-find-substring-in-c-using-library-functions","tag-how-to-find-substring-in-c-without-using-library-functions","tag-how-to-use-string-in-c-programming","tag-int-to-string-c","tag-program-to-find-substring-in-a-string-in-java","tag-string-c","tag-string-compare-c","tag-string-in-c-programming","tag-string-to-int-c","tag-substring-function-in-c","tag-the-c-string","tag-what-is-string-in-c-programming"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26584","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=26584"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26584\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26584"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26584"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26584"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}