<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Dayanand R on Medium]]></title>
        <description><![CDATA[Stories by Dayanand R on Medium]]></description>
        <link>https://medium.com/@dayanand.r?source=rss-ef304af1e38c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*rVsrCBFhyFfKt9oc</url>
            <title>Stories by Dayanand R on Medium</title>
            <link>https://medium.com/@dayanand.r?source=rss-ef304af1e38c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 10:42:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@dayanand.r/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Character sequence in a string : Sub-sequence using C]]></title>
            <link>https://medium.com/hackgenius/character-sequence-in-a-string-sub-sequence-using-c-47e8e6d56cfd?source=rss-ef304af1e38c------2</link>
            <guid isPermaLink="false">https://medium.com/p/47e8e6d56cfd</guid>
            <category><![CDATA[pointers]]></category>
            <category><![CDATA[c-programming]]></category>
            <category><![CDATA[subsequence]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[string-examination]]></category>
            <dc:creator><![CDATA[Dayanand R]]></dc:creator>
            <pubDate>Sat, 20 Jun 2020 01:19:02 GMT</pubDate>
            <atom:updated>2020-06-20T09:13:54.096Z</atom:updated>
            <content:encoded><![CDATA[<h3>Character Sequence in a String : Sub-sequence using C</h3><blockquote>Sub-sequence is another <strong><em>finding the needle in a haystack</em></strong> problem. The difference is that the characters must be in sequence. That’s the whole point , we have to find the sub-sequence(needle) in the string(hay).</blockquote><p>This is a problem which can be faced with much efficiency if you know the concept of pointers and recursion. You must be wondering why something as confusing as pointers are so commonly used in C is rarely mentioned. And why C has pointers.</p><h3>Why C has Pointers?</h3><p>C was developed when computers were much less powerful than they are today. And being very efficient with speed and memory usage was often not just desirable but vital. The raw ability to work with particular memory locations was obviously a useful option to have. A few tasks these days, such as programming micro-controllers, still need this. Most modern programmers do not need such fine control. The complications of using pointers make programs less clear to understand and add to the ways in which they can be go wrong. So why are pointers still used so much in C &amp; its successor, C++?</p><p>The reason is that pointers are used to bodge into C’s some vital features which are missing from the original language: arrays, strings, &amp; writable function parameters. They can also be used to optimize a program to run faster or use less memory than it would otherwise.</p><p>A pointer could be used for any, several or all these different reasons with little or no distinction in the language. So, if a programmer has not put in helpful comments, it will be difficult for others to follow through the program to see what each pointer is used for in order to work out why it is there instead of a plain simple variable.</p><p>So if you are satisfied with my answer…..</p><h3>let’s move on to the coding</h3><p><strong>Problem:</strong> There are two strings <strong>needle</strong> and <strong>haystack<em> (or hay)</em></strong><em>. Y</em>ou need to check if all the characters in needle are present in hay in a sequence i.e. if needle is a sub-sequence of the string. If yes then return <strong>True (1)</strong> or <strong>False (0)</strong></p><p>We can solve this problem in two ways:</p><ol><li><strong>Non-Recursive method</strong></li><li><strong>Recursive method</strong></li></ol><h4>1.Non-Recursive method:</h4><p>The <strong>Non-recursive</strong> method has a simple code with a <strong><em>do-while</em></strong> loop.</p><ul><li>Let’s start by creating a function which takes two strings as parameter. Let’s call it as <strong><em>subseq. </em></strong>Let the return type be int as we will be returning in binary.</li></ul><pre>int subseq(const char *needle, const char *hay){<br>    return 1;<br>}</pre><ul><li>Next with a do-while loop till hay is not empty, we will check if needle is not null and present in hay then increment needle to the next character.</li></ul><pre>int subseq(const char *needle, const char *hay){</pre><pre>    do {<br>        if (*needle &amp;&amp; tolower(*needle) == tolower(*hay))<br>          needle++;<br>    } while (*hay++);<br>    return !*needle;<br>}</pre><ul><li>The loop will run till the end the string hay and if all the characters of needle are present in hay in a sequence then the function will return True(1) or else False(0).</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/475/1*4W_1n4GxAAfJbiWdk4cOMg.png" /><figcaption>Image of code without comment</figcaption></figure><ul><li>Let’s <a href="https://tinyurl.com/subseq">visualize the code using python tutor</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8PIKJIXhi6BWGJev7xeOFA.png" /><figcaption><a href="https://tinyurl.com/subseq">Python Tutor visualization of the code</a></figcaption></figure><p>2. Recursive method:</p><p>The recursive method is very short code but a little complex.</p><ul><li>Let’s start by creating the function which takes two strings as parameters. Let’s call it as <strong><em>subseq_rec.</em></strong></li></ul><pre>int subseq_rec(const char *needle, const char *hay){<br>    return 1;<br>}</pre><ul><li>Next is the one line code which checks for the needle sequence in hay and returns True if the needle is <strong><em>sub-sequence</em></strong> and otherwise False</li></ul><pre>const char* strichr(const char *hay, const char needle){<br>    const char needlei = tolower(needle);<br>    do{<br>        if (tolower(*hay) == needlei){<br>            return hay;<br>        }<br>    }while(*hay++);</pre><pre>return NULL;<br>}<br>int subseq_rec(const char *needle, const char *hay) {<br>    return (*needle == 0 ? 1 : (strichr(hay, *needle) ) ?<br>    subseq(++needle,++hay) : 0 );<br>}</pre><ul><li>The code first checks for if there is any character is present in needle or not and then returns True if NULL.</li><li>If needle is not NULL then the code checks for if the first character of needle is present in hay or not using the function <strong><em>strichr</em></strong> then returns the function in a recursion by pointing the pointers to the next characters.</li><li>Strichr is similar to strchr function but unlike strchr it is case insensitive.</li><li>Here both hay and needle are incremented so that there is no repetition of character in hay and sequence is maintained.</li><li>If the sequence is broken then immediately the function returns False(0)</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/555/1*dTHTFRg8Fdk5At6nX3S6rQ.png" /><figcaption>Full Image of code</figcaption></figure><ul><li>Let’s <a href="https://tinyurl.com/subseq-rec">visualize the code using python tutor</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0uBY7qto7mxLJ0-Engb6zg.png" /><figcaption><a href="https://tinyurl.com/subseq-rec">Python Tutor visualization of the code</a></figcaption></figure><p>Lastly, I would like to thank my programming mentors at <a href="https://www.kgkite.ac.in/"><strong>KGiSL Institute of Technology</strong> (KiTE)</a> who supported me and encouraged me to put out this successful attempt at solving this problem and also for teaching me the <strong>fundamentals</strong> that were required to solve this problem.</p><p>And if you’ve understood the <strong>strichr</strong> then try this <a href="https://tinyurl.com/striRchr">striRchr</a> by <a href="https://medium.com/@_PragadeshMahalingam">Pragadesh Mahalingam</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=47e8e6d56cfd" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackgenius/character-sequence-in-a-string-sub-sequence-using-c-47e8e6d56cfd">Character sequence in a string : Sub-sequence using C</a> was originally published in <a href="https://medium.com/hackgenius">hackgenius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Finding a Needle in a Haystack(Subset) using C]]></title>
            <link>https://medium.com/hackgenius/finding-a-needle-in-a-haystack-subset-using-c-8dd8c17db434?source=rss-ef304af1e38c------2</link>
            <guid isPermaLink="false">https://medium.com/p/8dd8c17db434</guid>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[string-examination]]></category>
            <category><![CDATA[c-language]]></category>
            <category><![CDATA[pointers]]></category>
            <category><![CDATA[c-programming-language]]></category>
            <dc:creator><![CDATA[Dayanand R]]></dc:creator>
            <pubDate>Fri, 19 Jun 2020 15:31:50 GMT</pubDate>
            <atom:updated>2020-06-20T09:11:44.385Z</atom:updated>
            <content:encoded><![CDATA[<blockquote><em>Ever heard of a word </em>strenuosity <em>? Well finding a needle in a haystack can be one strenuous work</em></blockquote><p>Searching for a needle in a haystack and are at your wits end 😔? Then you have come to the right place. Well, this problem might be <em>confusing</em>😕, <em>difficult</em>😫or <em>impossible</em>😱 for you. Unless you understand the concept for pointer and recursion.</p><p>For some pointer is like an alien 👽 object which is incomprehensible. But pointers in C are easy and fun to learn. Some C programming tasks are performed more easily with pointers, and other tasks, such as dynamic memory allocation, cannot be performed without using pointers. So it becomes necessary to learn pointers to become a perfect C programmer.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/360/1*vbkSvzj6IlzcnyvZIlllQg.png" /></figure><h4>What are Pointers?</h4><p>A <strong>pointer</strong> is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.<br>It’s general form is:</p><pre>type *var-name; //<strong>type</strong> is the pointer&#39;s base type and var-name is  <br>                //the variable name</pre><p>It’s not that no one knows how to teach about pointers. It is not that no one can understand pointers or that you need a separate brain part to understand pointer. It all comes down to one&#39;s way of understanding it, or how much they truly want to learn pointers. One’s conviction to learn is something which determines if he/she as the ability to learn something or not.</p><p>As our coding mentors says: “It all comes down to <strong>perfect practice</strong> and <strong>effort</strong>.”</p><h3>Let’s move on to the coding</h3><p><strong>Problem:</strong> There are two strings <strong>needle</strong> and <strong>haystack<em> (or hay)</em></strong><em>. Y</em>ou need to check if all the characters in the needle are present in haystack or not. If yes then return <strong>True (1)</strong> or <strong>False (0)</strong></p><p>We can solve this problem in three ways. They all are similar in a sense but there is a significant difference and they are :</p><ol><li><strong>Non-recursive</strong> method using <strong>string functions</strong>.</li><li><strong>Non-recursive</strong> method without using <strong>string function.</strong></li><li><strong>Recursive</strong> method.</li></ol><h4>1. <strong>Non-recursive</strong> method using <strong>string functions</strong> :</h4><p>The non-recursive method is quite simple. We are using a single <strong><em>while loop</em></strong> and <strong><em>strchr</em></strong> function.</p><ul><li>Let’s start by creating a function, which takes two strings as parameters. Let’s name it as <strong><em>subset_str , </em></strong>as string function is being used. Let the return type be int as we are returning 0(False) or 1(True)</li></ul><pre>int subset_str(const char *needle, const char *hay){<br>    return 1;<br>}</pre><ul><li>Our first test case will be to check whether hay is null. If yes then return 0.</li></ul><pre>int subset_str(const char *needle, const char *hay){<br>    if(!hay)<br>        return 0;<br>    return 1;<br>}</pre><ul><li>Next step is the code where we actually check for the needle.</li></ul><pre>int subset_str(const char *needle, const char *hay){<br>    if(!hay)<br>        return 0;<br>    while(*needle){<br>        if(!strchr(hay, *needle)) //search for the character <br>                                  //in haystack and returns      <br>                                  //zero if not found<br>            return 0;<br>        needle++;     // if found increment needle to the next <br>                      //character<br>    }<br>    return 1; //control will reach this point only when all the <br>              //characters in needle were present in haystack so <br>              //return 1<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/438/1*Qutc2GcuaYl4k6RQCQG4yw.png" /><figcaption>Image of code without comment</figcaption></figure><ul><li>Let’s <a href="https://tinyurl.com/subset-str">visualize this code using Python Tutor</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*r_KAUBUW6dfr2yFgCFeR1w.png" /><figcaption><a href="https://tinyurl.com/subset-str">Python Tutor visualization of the code</a></figcaption></figure><h4>2. <strong>Non-recursive</strong> method without using <strong>string function:</strong></h4><p>The second non-recursive method doesn’t need any string function, though the size of the code increases.</p><ul><li>Let’s again start by creating a function, which takes two strings as parameters. Let’s name it as <strong><em>subset. </em></strong>And the return type is <strong>int</strong>.</li></ul><pre>int subset(const char *needle, const char *hay){<br>    return 1;<br>}</pre><ul><li>Again our First test case will be to check whether hay is null. If yes then return 0.</li></ul><pre>int subset(const char *needle, const char *hay){<br>    if(!hay &amp;&amp; !*hay)<br>        return 0;<br>    return 1;<br>}</pre><ul><li>Next, we’ll check if all the characters of the needle are present in hay or not using a <strong>do-while</strong> loop.</li><li>We’ll use a variable <em>start</em> so that when the character is found in hay we can bring it back to the start of the string.</li></ul><pre>int subset(const char *needle, const char *hay){<br>    if(!hay)<br>        return 0;<br>    do{<br>       if{*needle &amp;&amp; *needle == *hay){ /*if there is a character in<br>                                         needle and that character <br>                                         is present in hay then */<br>          needle++, hay = start;       /*needle will be incremented <br>                                         to the next character and <br>                                         hay will be brought back to<br>                                         the start of the string*/<br>           <br>       }<br>    } while(*hay++);  // this loop will run till all the characters <br>                     // of needle are found or all the character of<br>                     // hay are checked<br>    return !needle;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/417/1*pQNxUnOCApNCosDtzoV0sA.png" /><figcaption>Image of code without comment</figcaption></figure><ul><li>Let’s <a href="https://tinyurl.com/subset-nsf">visualize this code using Python Tutor</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1012/1*h_6fNvbPGRuH7gCr7gO1_A.png" /><figcaption>Python Tutor visualization of the code</figcaption></figure><h4>3. <strong>Recursive</strong> method :</h4><p>The third and last method is the recursive method. This method is like all two other methods. We will name it as <strong><em>subset_rec</em></strong>.</p><ul><li>Like the earlier two, we will start off by checking for hay.</li></ul><pre>int subset_rec(const char *needle, const char *hay){<br>    if(!hay){<br>        return 0;<br>    }<br>}</pre><ul><li>Next, we will check if the characters of needle are present in hay or not.</li></ul><pre>int subset_rec(const char *needle, const char *hay){<br>    int start = hay;<br>    if(!hay){<br>        return 0;s<br>    }</pre><pre>    do {<br>        if ( *needle &amp;&amp; *needle == *hay){/*if there is a character<br>                                           in needle and that      <br>                                           character is present in <br>                                           hay then increment needle<br>                                           to next character*/<br>            needle++;<br>            return subset_rec(needle,start);/*and return needle and <br>                                           hay(back from first    <br>                                          character) to subset_rec*/<br>        }<br>            <br>    } while(*hay++);<br>    <br>    return !*needle;<br>}</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/463/1*ohu_ctrn2G5YZWpw8o_xPw.jpeg" /><figcaption>Image of code without comment</figcaption></figure><ul><li>Let’s<a href="https://tinyurl.com/subset-rec"> visualize the code using python Tutor</a></li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1l7UQcjk5bt0WLRbP2ksQA.png" /><figcaption><a href="https://tinyurl.com/subset-rec">Python Tutor visualization of the code</a></figcaption></figure><p>Lastly, I would like to thank my coding mentors at <a href="https://www.kgkite.ac.in/"><strong>KGiSL Institute of Technology</strong> (KiTE)</a> who encouraged me to put out this successful attempt at solving this problem and also for teaching me the <strong>fundamentals</strong> that were required to solve this.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8dd8c17db434" width="1" height="1" alt=""><hr><p><a href="https://medium.com/hackgenius/finding-a-needle-in-a-haystack-subset-using-c-8dd8c17db434">Finding a Needle in a Haystack(Subset) using C</a> was originally published in <a href="https://medium.com/hackgenius">hackgenius</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>