{"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&#8217;t 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=&#8221;banner&#8221;]\n<h2 id=\"c-programming-code\">C programming code<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include &lt;stdio.h&gt;<br\/>#include &lt;string.h&gt;<br\/> <br\/>int check_subsequence (char [], char[]);<br\/> <br\/>int main () {<br\/>   int flag;<br\/>   char s1[1000], s2[1000];<br\/> <br\/>   printf(&quot;Input first string\\n&quot;);<br\/>   gets(s1);<br\/> <br\/>   printf(&quot;Input second string\\n&quot;);<br\/>   gets(s2);<br\/> <br\/>   \/** Passing smaller length string first *\/<br\/> <br\/>   if (strlen(s1) &lt; strlen(s2))<br\/>      flag = check_subsequence(s1, s2);<br\/>   else<br\/>      flag = check_subsequence(s2, s1);<br\/> <br\/>   if (flag)<br\/>      printf(&quot;YES\\n&quot;);<br\/>   else<br\/>      printf(&quot;NO\\n&quot;);<br\/> <br\/>   return 0;<br\/>}<br\/> <br\/>int check_subsequence (char a[], char b[]) {<br\/>   int c, d;<br\/> <br\/>   c = d = 0;<br\/> <br\/>   while (a[c] != &#039;\\0&#039;) {<br\/>      while ((a[c] != b[d]) &amp;&amp; b[d] != &#039;\\0&#039;) {<br\/>         d++;<br\/>      }<br\/>      if (b[d] == &#039;\\0&#039;)<br\/>         break;<br\/>      d++;<br\/>      c++;<br\/>   }<br\/>   if (a[c] == &#039;\\0&#039;)<br\/>      return 1;<br\/>   else<br\/>      return 0;<br\/>}<\/code><\/pre> <\/div>\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=&#8221;banner&#8221;]\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}]}}