title: Using Recursion to compare 2 strings in C
body: <p>I am required to write a program that uses recursion to compare 2 strings say S1 and S2 which returns 1 , 0 and -1 for s1 &gt; s2, s1 == s2 and s1 &lt; s2 respectively.</p>
  <p>For this assignment that I am tasked, I cannot use any string.h library unfortunately.</p>
  <p>I am a beginner in recursion and got the hang of dealing with integer / array scenarios. However, when dealing with these strings, I cannot seem to establish the &quot;Terminating condition&quot; and the &quot;Recursive condition&quot; and I seek everyone's kind guidance on this matter.</p>
  <p>The main body is provided as a template by my professor so there really isn't anything I can do to edit this portion of it.</p>
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  
  #define INIT_VALUE 100
  
  int rStrcmp(char *s1, char *s2);
  
  int main()
  {
   char source[40], target[40], *p;
   int result = INIT_VALUE;
  
   printf(&quot;Enter a source string: \n&quot;);
   fgets(source, 40, stdin);
   if (p=strchr(source,'\n')) *p = '\0';
  
   printf(&quot;Enter a target string: \n&quot;);
   fgets(target, 40, stdin);
   if (p=strchr(target,'\n')) *p = '\0';
  
   result = rStrcmp(source, target);
  
   printf(&quot;rStrcmp(): %d&quot;, result);
   return 0;
  } 
  </code></pre>
  <p>The recursive code that I have written so far:</p>
  <pre><code>int rStrcmp(char *s1, char *s2)
  {
        // The terminating conditions:
  
        if (*s1 == '\0' &amp;&amp; *s2 == '\0') //  both strings end together
           return 0;
        else if (*s1 == '\0') // s1 ends first (s1 smaller)
           return ‐1;
        else if (*s2 == '\0')  // s2 ends first (s1 larger)
           return 1;
        else if (*s1 &lt; *s2) // ASCII char in s1 &lt; s2 : (s1 smaller)
           return ‐1;
        else if (*s1 &gt; *s2)  // ASCII char in s1 &gt; s2 : (s1 larger)
           return 1;     
  
        else // recursive condition
      {
         rStrcmp(s1+1,s2+1) // moves the address to point to the next char 
      }
  }
  </code></pre>
  <p>I think I roughly know what my mistake is, that is I am comparing the entire string in the address instead of the individual characters. Is there anyway I could write the program so that I am comparing each characters in both strings?</p>
  <p>Seeking everyone's kind guidance on this matter. I can solve this using iteration easily but I have to solve it using recursion and I can't really understand recursion with strings.</p>
  <p>EDIT:
  This program can compile and run. However, when I input test cases such as:</p>
  <p>s1 = abc123
  s2 = abc123f</p>
  <p>The output returns a 1. Which should actually be a -1 instead.</p>
  <p>or</p>
  <p>s1 = abc123
  s2 = abcdef</p>
  <p>The output returns a 1. Which should actually be a -1 as well since the character '1' in s1 has a lower ASCII value than 'd' in s2.</p>