{"id":26587,"date":"2017-12-21T20:09:18","date_gmt":"2017-12-21T14:39:18","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26587"},"modified":"2017-12-21T20:09:18","modified_gmt":"2017-12-21T14:39:18","slug":"c-program-to-check-subsequence","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-program-to-check-subsequence\/","title":{"rendered":"C program to check subsequence"},"content":{"rendered":"<p>C program to check Subsequence, don\u2019t confuse subsequence with <a href=\"https:\/\/www.wikitechy.com\/technology\/substring-in-c-programming\/\">substring<\/a>. In our program we check if a string is a subsequence of another string. User will input two strings and we find if one of the strings is a subsequence of other. Program prints yes if either first string is a subsequence of second or second is a subsequence of first. We pass smaller length string first because our function assumes first string is of smaller or equal length than the second string.<\/p>\n[ad type=\u201dbanner\u201d]\n<h2 id=\"c-programming-code\">C programming code<\/h2>\n[pastacode lang=\u201dc\u201d manual=\u201d%23include%20%3Cstdio.h%3E%0A%23include%20%3Cstring.h%3E%0A%20%0Aint%20check_subsequence%20(char%20%5B%5D%2C%20char%5B%5D)%3B%0A%20%0Aint%20main%20()%20%7B%0A%20%20%20int%20flag%3B%0A%20%20%20char%20s1%5B1000%5D%2C%20s2%5B1000%5D%3B%0A%20%0A%20%20%20printf(%22Input%20first%20string%5Cn%22)%3B%0A%20%20%20gets(s1)%3B%0A%20%0A%20%20%20printf(%22Input%20second%20string%5Cn%22)%3B%0A%20%20%20gets(s2)%3B%0A%20%0A%20%20%20%2F**%20Passing%20smaller%20length%20string%20first%20*%2F%0A%20%0A%20%20%20if%20(strlen(s1)%20%3C%20strlen(s2))%0A%20%20%20%20%20%20flag%20%3D%20check_subsequence(s1%2C%20s2)%3B%0A%20%20%20else%0A%20%20%20%20%20%20flag%20%3D%20check_subsequence(s2%2C%20s1)%3B%0A%20%0A%20%20%20if%20(flag)%0A%20%20%20%20%20%20printf(%22YES%5Cn%22)%3B%0A%20%20%20else%0A%20%20%20%20%20%20printf(%22NO%5Cn%22)%3B%0A%20%0A%20%20%20return%200%3B%0A%7D%0A%20%0Aint%20check_subsequence%20(char%20a%5B%5D%2C%20char%20b%5B%5D)%20%7B%0A%20%20%20int%20c%2C%20d%3B%0A%20%0A%20%20%20c%20%3D%20d%20%3D%200%3B%0A%20%0A%20%20%20while%20(a%5Bc%5D%20!%3D%20\u2019%5C0\u2032)%20%7B%0A%20%20%20%20%20%20while%20((a%5Bc%5D%20!%3D%20b%5Bd%5D)%20%26%26%20b%5Bd%5D%20!%3D%20\u2019%5C0\u2032)%20%7B%0A%20%20%20%20%20%20%20%20%20d%2B%2B%3B%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20if%20(b%5Bd%5D%20%3D%3D%20\u2019%5C0\u2032)%0A%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20d%2B%2B%3B%0A%20%20%20%20%20%20c%2B%2B%3B%0A%20%20%20%7D%0A%20%20%20if%20(a%5Bc%5D%20%3D%3D%20\u2019%5C0\u2032)%0A%20%20%20%20%20%20return%201%3B%0A%20%20%20else%0A%20%20%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output of program:<\/strong><\/p>\n<pre class=\"c geshifilter-c\">Input first string\r\ncomputer science is awesome\r\nInput second string\r\ntree\r\nYES<\/pre>\n[ad type=\u201dbanner\u201d]\n<p>The logic of function is simple we keep on comparing characters of two strings, if mismatch occur then we move to next character of second string and if characters match indexes of both strings is increased by one and so on. If the first string ends then it is a subsequence otherwise not.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C program to check subsequence &#8211; C Programming &#8211; User will input two strings and we find if one of the strings is a subsequence of other.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,69866],"tags":[79231,79219,79211,79227,79224,79228,79232,79215,79221,73002,79217,79218,79220,79216,79223,72996,79225,72997,79226,73560,79222,79214,79212,79213,79229,79230],"class_list":["post-26587","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-c-programming","tag-all-subsequence-of-a-string","tag-all-subsequences-of-a-string","tag-c-program-to-check-subsequence","tag-check-if-a-string-is-a-substring-of-another-string-in-c","tag-distinct-subsequences","tag-find-all-subsequences-of-a-string","tag-find-the-number-of-occurrences-of-a-subsequence-in-a-string","tag-longest-common-subsequence","tag-longest-common-subsequence-algorithm","tag-longest-common-subsequence-applications","tag-longest-common-subsequence-c-code","tag-longest-common-subsequence-code","tag-longest-common-subsequence-dynamic-programming","tag-longest-common-subsequence-dynamic-programming-code","tag-longest-common-subsequence-example","tag-longest-common-subsequence-java","tag-longest-common-subsequence-java-dynamic-programming","tag-longest-common-subsequence-program-in-c","tag-longest-common-subsequence-program-in-c-with-output","tag-longest-palindromic-subsequence","tag-longest-subsequence","tag-string-subsequence","tag-subsequence","tag-subsequence-java","tag-subsequence-of-a-string-in-c","tag-subsequence-of-a-string-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26587","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=26587"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26587\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}