home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch5 / strnglib.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  2KB  |  54 lines

  1. /*                         strnglib.c
  2.  *
  3.  *   Synopsis  - Displays information about two strings, 
  4.  *               the length, and their lexical comparison. Then
  5.  *               concatenates them into a character buffer and
  6.  *               displays the result.
  7.  *
  8.  *   Objective - To illustrate basic use of the string library
  9.  *               functions, strlen(), strcat(), strcpy(), and
  10.  *               strcmp().
  11.  */
  12.  
  13. /* Include Files */
  14. #include <stdio.h>
  15. #include <string.h>                                   /* Note 1 */
  16.  
  17. /* Constant Definitions */
  18. #define BUF_SIZE 512
  19.  
  20. int main( void )
  21. {
  22.      char workstring[BUF_SIZE];                       /* Note 2 */
  23.      char *string1 = "I know an old lady";
  24.      char *string2 = "who swallowed a fly";
  25.  
  26.      printf( string1 );
  27.      printf( "\n" );
  28.      printf( string2 );
  29.      printf( "\n" );
  30.      if ( strcmp( string1, string2 ) > 0 )            /* Note 3 */
  31.           printf( "string1 is > string2.\n" );
  32.      else
  33.           printf( "string1 is <= string2.\n" );
  34.  
  35.                                                       /* Note 4 */
  36.      printf( "The length of string1 is %d.\n", strlen( string1 ) );
  37.      printf( "The length of string2 is %d.\n", strlen( string2 ) );
  38.  
  39.      strcpy( workstring, string1 );                   /* Note 5 */
  40.      if ( !strcmp( string1, workstring ) )            /* Note 6 */
  41.           printf( "Copy completed successfully!\n" );
  42.      else
  43.           printf( "Error found in copy.\n" );
  44.  
  45.      strcat( workstring, " " );                       /* Note 7 */
  46.      strcat( workstring, string2 );
  47.      printf( "The work string now contains:\n\t\"%s\"\n",
  48.                                                 workstring );
  49.      printf( "The length of the work string is now %d.\n",
  50.                                                 strlen( workstring ) );
  51.      return 0;
  52. }
  53.  
  54.