/************************************************************************
 *									*
 *		   Comparing Two Character Arrays (Strings)		*
 *		   ========================================		*
 *									*
 *	  Copyright © 1993 by Mark Little, All Rights Reserved		*
 *									*
 * This source and program are provided as part of the OZAmiga Cover	*
 * Disk and may not be used in any other publication or training course *
 * without the written permission of the Author.			*
 *									*
 ************************************************************************
 *									*
 * Function								*
 * ========								*
 *									*
 * This program demonstrates how to compare two character arrays	*
 * (strings) the hard way and the easy way.				*
 *									*
 ************************************************************************
 *									*
 * Source File:		  Compare.c					*
 * Source Version:	  1.0						*
 * Destination File:	  Compare					*
 *									*
 * Source Computer:	  Amiga 3000					*
 * Operating System:	  AmigaDOS					*
 * O/S Version:		  2.04						*
 * Compiler:		  SAS C						*
 * Version:		  6.2						*
 * Destination Computer:  Any Amiga					*
 *									*
 ************************************************************************
 *									*
 * Revision History							*
 * ================							*
 *									*
 * 07-Mar-93								*
 * ---------								*
 *									*
 * Original Version							*
 *									*
 ************************************************************************/

/************************************************************************
 *			INCLUDE Files					*
 ************************************************************************
 These files usually define external functions which will be called by the
 program. They can be functions supplied with the compiler, or they can
 be functions you have written. File names enclosed in angle brackets will
 be looked for along the search path, while file names enclosed in double
 quotes (") will be looked for in the current directory only.
 ************************************************************************/

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

/************************************************************************
 *			Definations					*
 ************************************************************************
 These lines define structures which will be used in the program.
 This allows changes to be made in many places by only changing one line.
 ************************************************************************/

/************************************************************************
 *			GLOBAL Variables				*
 ************************************************************************
 Variables defined outside of the program modules can be accessed by all
 program modules in this file and by other modules which are linked to
 this file. Variables defined inside the opening braces of a program module
 are only accessable by that module.
 ************************************************************************/

/*======================================================================*
 *			Force a Larger WorkBench Window			*
 *======================================================================*
 *** CAUTION *** This following statements are designed for the SAS C
 compiler version 6.2 and defines the type of window to be opened if this
 program is run from WorkBench instead of the CLI. To stop this from being
 compiled by other compilers, it is enclosed in a conditional clause.
 My compiler defines __SASC_60 which means this statement will be compiled
 into the program.
 You should also remember that some compilers may produce programs which
 may crash if they are run from the WorkBench when they should be run from
 the CLI - Rebuild this program and see what happens!
 *======================================================================*/

#ifdef __SASC_60
                  char        __stdiowin[] =
                                "CON:0/0/640/200/Learning C - Compare";
#endif

/*======================================================================*
 *			Program Variables				*
 *======================================================================*
 These statements tell the compiler what the names of the variables used
 in this program are. They also define what type of information is to be
 stored in the variables.
 *======================================================================*/

   int       i;
   char     *Pointer1,
            *Pointer2,
             String1[80],
             String2[80],
             First[] = "FIRSt",
             Next[]  = "FIRST";

/************************************************************************
 *			MAIN PROGRAM					*
 ************************************************************************
 The module named "main" is the starting point of execution. Your compiler
 may open resources such as DOS, EXEC and other libraries before main is
 called. Consult your compiler documentation for details of what it can
 do.
 ************************************************************************/

void main(void)
{

/*======================================================================*
 *			Compare Two Equal Length Strings		*
 *======================================================================*
 This routine will compare two character arrays (strings) which are the
 same length. Normally, you wouldn't use this method as strings often vary
 in length, but this is done as a demo only.
 *======================================================================*/

   Pointer1 = First;
   Pointer2 = Next;
   printf("%s is the first string, %s is the next string\n\n",First,Next);
   for (i = 0; i < 5; i++)    /* Test from postion 0 to 5               */
   {
      if (Pointer1[i] != Pointer2[i])
      {
         printf("The Strings don't Match!\n\n");
         break;
      }
   }

/*======================================================================*
 *			Read in Two Strings				*
 *======================================================================*
 Read in two strings from the console for the next tests.
 *======================================================================*/

   printf("Your turn to compare two strings using functions\n");
   printf("Enter the first string: ");
   scanf("%s",String1);
   printf("Enter the second string: ");
   scanf("%s",String2);

/*======================================================================*
 *			Now Compare Strings the Easy Way		*
 *======================================================================*
 Now let's compare strings as we normally would. First we do an exact
 comparison. This means that the case of the strings is significant. The
 "strcmp" function will compare two strings and return the following as a
 result.
 
 NEGATIVE         First String is less than the Second
 ZERO             First String is an exact match of the Second
 POSITIVE         First String is greater than the Second

 *======================================================================*/

   if (strcmp(String1,String2) == 0)
      printf("This is a perfect match!\n");

/*======================================================================*
 *			If the Match is Not Perfect, Try Again		*
 *======================================================================*
 This time we try to compare ignoring the case of the letters. This means
 that "THE" will match "the". This means that the case of the strings is
 not significant. The "strcmp" function will compare two strings and
 return the following as a result.
 
 NEGATIVE         First String is less than the Second (ignoring case)
 ZERO             First String is an exact match of the Second (except for
                        case of the letters)
 POSITIVE         First String is greater than the Second (ignoring case)

 *======================================================================*/

   else
   {
      if (stricmp(String1,String2) == 0)
         printf("These match if you ignore the case\n");
      else
         printf("I'm sorry, these strings don't match!\n");
   }
}
