home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / cmpstr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-25  |  2.5 KB  |  84 lines

  1. /* CMPSTR.C illustrates string and memory comparison functions including:
  2.  *       memcmp        memicmp
  3.  *       strncmp       strnicmp
  4.  *       strcmp        stricmp          strcmpi
  5.  */
  6.  
  7. #include <memory.h>
  8. #include <string.h>
  9. #include <stdio.h>
  10.  
  11. char string1[] = "The quick brown dog jumps over the lazy fox";
  12. char string2[] = "The QUICK brown fox jumps over the lazy dog";
  13.  
  14. main()
  15. {
  16.     char tmp[20];
  17.     int result;
  18.  
  19.     printf( "Compare strings:\n\t\t%s\n\t\t%s\n\n", string1, string2 );
  20.  
  21.     printf( "Function:\tmemcmp\n" );
  22.     result = memcmp( string1, string2 , 42 );
  23.     if( result > 0 )
  24.         strcpy( tmp, "greater than" );
  25.     else if( result < 0 )
  26.         strcpy( tmp, "less than" );
  27.     else
  28.         strcpy( tmp, "equal to" );
  29.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );
  30.  
  31.     printf( "Function:\tmemicmp\n" );
  32.     result = memicmp( string1, string2, 42 );
  33.     if( result > 0 )
  34.         strcpy( tmp, "greater than" );
  35.     else if( result < 0 )
  36.         strcpy( tmp, "less than" );
  37.     else
  38.         strcpy( tmp, "equal to" );
  39.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );
  40.  
  41.     printf( "Function:\tstrncmp\n" );
  42.     result = strncmp( string1, string2 , 42 );
  43.     if( result > 0 )
  44.         strcpy( tmp, "greater than" );
  45.     else if( result < 0 )
  46.         strcpy( tmp, "less than" );
  47.     else
  48.         strcpy( tmp, "equal to" );
  49.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );
  50.  
  51.     printf( "Function:\tstrnicmp\n" );
  52.     result = strnicmp( string1, string2, 42 );
  53.     if( result > 0 )
  54.         strcpy( tmp, "greater than" );
  55.     else if( result < 0 )
  56.         strcpy( tmp, "less than" );
  57.     else
  58.         strcpy( tmp, "equal to" );
  59.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );
  60.  
  61.     printf( "Function:\tstrcmp\n" );
  62.     result = strcmp( string1, string2 );
  63.     if( result > 0 )
  64.         strcpy( tmp, "greater than" );
  65.     else if( result < 0 )
  66.         strcpy( tmp, "less than" );
  67.     else
  68.         strcpy( tmp, "equal to" );
  69.     printf( "Result:\t\tString 1 is %s than string 2\n\n", tmp );
  70.  
  71.     printf( "Function:\tstricmp or strcmpi\n" );
  72.     result = stricmp( string1, string2 );
  73.     /* strcmpi (commented out) is the same as stricmp.
  74.     result = strcmpi( string1, string2 );
  75.      */
  76.     if( result > 0 )
  77.         strcpy( tmp, "greater than" );
  78.     else if( result < 0 )
  79.         strcpy( tmp, "less than" );
  80.     else
  81.         strcpy( tmp, "equal to" );
  82.     printf( "Result:\t\tString 1 is %s than string 2\n", tmp );
  83. }
  84.