Exercise 10-7 - CBD page 355 - iterative version, Kurt Nørmark

Solution index                   Textual C program


/* Programmed by Kurt Nørmark, April 2003 */

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

/* Assume as a precondition that both s1 and s2 are '0' terminated strings */
int mystrcmp(const char *s1, const char *s2){
  char *s1_ptr, *s2_ptr;
  int result, done;

  s1_ptr = s1; s2_ptr = s2;

  do {
    done = 1;    /* done in this iteration unless we change our mind */
    if (*s1_ptr == '\0' && *s2_ptr == '\0') 
      result = 0;
    else if (*s1_ptr == '\0' && *s2_ptr != '\0')
      result = -1;
    else if (*s1_ptr != '\0' && *s2_ptr == '\0')
      result = 1;
    else if (*s1_ptr < *s2_ptr)
      result = -1;
    else if (*s1_ptr > *s2_ptr)
      result = 1;
    else{
      s1_ptr++; s2_ptr++; done = 0;
    }
  }
  while (!done);

  return result;
}

void compare(char *s1, char *s2){
  printf("strcmp(\"%s\",\"%s\") = %i, %i\n", s1, s2, mystrcmp(s1,s2), strcmp(s1,s2));
}

int main(void) {

  compare("abe", "kat");
  compare("abe", "abe");
  compare("", "kat");
  compare("", "");
  compare("", "a");
  compare("a", "");
  compare("abe", "aben");
  compare("aben", "aben");
  
  return 0;
}

In this program we show an iterative version of the function strcmp. A similar recursive program is also available. Please, look at the recursive version first. We will only make minimal remarks below, because the two versions are quite similar. Both are made by Kurt Nørmark.
 

int mystrcmp(const char *s1, const char *s2){
  char *s1_ptr, *s2_ptr;
  int result, done;

  s1_ptr = s1; s2_ptr = s2;

  do {
    done = 1;    /* done in this iteration unless we change our mind */
    if (*s1_ptr == '\0' && *s2_ptr == '\0') 
      result = 0;
    else if (*s1_ptr == '\0' && *s2_ptr != '\0')
      result = -1;
    else if (*s1_ptr != '\0' && *s2_ptr == '\0')
      result = 1;
    else if (*s1_ptr < *s2_ptr)
      result = -1;
    else if (*s1_ptr > *s2_ptr)
      result = 1;
    else{
      s1_ptr++; s2_ptr++; done = 0;
    }
  }
  while (!done);

  return result;
}

void compare(char *s1, char *s2){
  printf("strcmp(\"%s\",\"%s\") = %i, %i\n", s1, s2, mystrcmp(s1,s2), strcmp(s1,s2));
}

int main(void) {
Overall, the body of mystrcmp consists of a do loop. This makes sense because we know there is at least one char in both s1 and s2 (namely the terminating null chars). So instead of recursion we iterate. The boolean (int) variable done keeps track of the need for repetition of the loop. In principle we ought to assign done in each branch of the if-else chain; Of convenience, however, we initially assign done to 1 (true), and change it to 0 (false) in the final branch. The local pointer variabels s1_ptr and s2_ptr are incremented in case we are not done.

I first made the recursive version, because from a problem solving point of view, it was the most natural for me to program. The program makes use of so-called tail recursion (recursion at the very end of the function) and of that reason it is easy to make an iterative solution from it. Following that I made the iterative one as similar as possible to the recursive version.
 


Generated: Wednesday, March 29, 2006, 12:33:05
This program dissection page is generated from an XML-in-LAML source file