Puzzle S11

Are two strings equal?

[E-4]Write a function that determines if two strings are equal. Two strings are equal if the strings are of equal length and have the same characters at the same locations. Two empty strings are equal. Here is a prototype for the function:

int areEqual( char const *left, char const *right );

The function returns 1 if the strings are equal, and zero otherwise. Warning: usually in C you would use the standard library function strcmp() to test for equality of strings. That function returns 0 when strings are equal. Here is a skeleton of a testing program. Finish it by completing the function.

/* Puzzle S11 -- are two strings equal? */

#include <stdio.h>
#include <stdlib.h>

int areEqual( char const *left, char const *right )
{
}

int main(int argc, char *argv[])
{
 char *trials[][2] =
  {
    {"a", "a"},
    {"a", "f"},
    {"abc", "abc"},
    {"abc", "def"},
    {"abcdef", "abcdef"},
    {"abcdef", "aaaaaa"},
    {"abcdeff", "abcdef"},
    {"abcdef", "xabcdef"},
    {"xabcdef", "abcdef"},
    {"abcdef", "rats"},
    {"abbccf", "cc"},
    {"abcdefg", "g"},
    {"apple", ""},
    {"", "apple"},
    {"",""},
    {"green", "green"},
    {"ASBC", "ASBC"},
    {"    ", "    "},
    {"12345", "12345"},
    {"++-!@#", "++-!@#"},
    {"tab\ttab", "tab\ttab"},
   };

  int j;
  for ( j=0; j<sizeof(trials)/sizeof(trials[0]); j++ )
  {
    printf("%s\t%s\t", trials[j][0], trials[j][1] );
    if ( areEqual( trials[j][0], trials[j][1] ) )
      printf("are     equal\n");
    else
      printf("are not equal\n");
  }

  return 0;
}


Answer         Next Page         Previous Page Home