简体   繁体   中英

Using Recursion to compare 2 strings in C

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 > s2, s1 == s2 and s1 < s2 respectively.

For this assignment that I am tasked, I cannot use any string.h library unfortunately.

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 "Terminating condition" and the "Recursive condition" and I seek everyone's kind guidance on this matter.

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.

#include <stdio.h>
#include <string.h>

#define INIT_VALUE 100

int rStrcmp(char *s1, char *s2);

int main()
{
 char source[40], target[40], *p;
 int result = INIT_VALUE;

 printf("Enter a source string: \n");
 fgets(source, 40, stdin);
 if (p=strchr(source,'\n')) *p = '\0';

 printf("Enter a target string: \n");
 fgets(target, 40, stdin);
 if (p=strchr(target,'\n')) *p = '\0';

 result = rStrcmp(source, target);

 printf("rStrcmp(): %d", result);
 return 0;
} 

The recursive code that I have written so far:

int rStrcmp(char *s1, char *s2)
{
      // The terminating conditions:

      if (*s1 == '\0' && *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 < *s2) // ASCII char in s1 < s2 : (s1 smaller)
         return ‐1;
      else if (*s1 > *s2)  // ASCII char in s1 > s2 : (s1 larger)
         return 1;     

      else // recursive condition
    {
       rStrcmp(s1+1,s2+1) // moves the address to point to the next char 
    }
}

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?

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.

EDIT: This program can compile and run. However, when I input test cases such as:

s1 = abc123 s2 = abc123f

The output returns a 1. Which should actually be a -1 instead.

or

s1 = abc123 s2 = abcdef

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.

The program you give us was not the one you really use, since it doesn't compile, a ; is missing. I guess it was present after the recursive rStrcmp call.

You should activate warning on you compiler, you'll find that

In function 'rStrcmp': .code.tio.c:60:1: warning: control reaches end of non-void function [-Wreturn-type] }

In fact, in function rStrcmp , the recursive line should be:

...
    else // recursive condition
    {
       return rStrcmp(s1+1,s2+1); /* the return was missing here */
    }
...

And as pointed in comments, you can simplify your code by removing the else since after a return, you're no longer in the function:

int rStrcmp(char *s1, char *s2)
{
    // The terminating conditions:
    if (*s1 == '\0' && *s2 == '\0') //  both strings end together
        return 0;
    if (*s1 == '\0') // s1 ends first (s1 smaller)
        return ‐1;
    if (*s2 == '\0')  // s2 ends first (s1 larger)
        return 1;
    if (*s1 < *s2) // ASCII char in s1 < s2 : (s1 smaller)
        return ‐1;
    if (*s1 > *s2)  // ASCII char in s1 > s2 : (s1 larger)
        return 1;     
    // recursive condition
    return rStrcmp(s1+1,s2+1); // moves the address to point to the next char 
}

Last move, you could add some formatting information in main function to have a nicer output:

result = rStrcmp(source, target);

printf("rStrcmp(): %d\n", result);

printf("'%s' %c '%s'\n", source, result == 0 ? '=' : result < 0 ? '<' : '>' , target);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM