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

  1. /* CPYSTR.C illustrate memory and string copy and move functions including:
  2.  *      memccpy         memcpy          memmove
  3.  *      strncpy         strcpy          strdup          strlen
  4.  */
  5.  
  6. #include <memory.h>
  7. #include <string.h>
  8. #include <stdio.h>
  9. #include <conio.h>
  10. #include <dos.h>
  11.  
  12. char string1[60] = "The quick brown dog jumps over the lazy fox";
  13. char string2[60] = "The quick brown fox jumps over the lazy dog";
  14. /*                           1         2         3         4         5 *
  15.  *                  12345678901234567890123456789012345678901234567890 */
  16. main()
  17. {
  18.     char buffer[61];
  19.     char *pdest, *newstring;
  20.     int  pos;
  21.  
  22.     printf( "Function:\tmemccpy 60 characters or to character 's'\n" );
  23.     printf( "Source:\t\t%s\n", string1 );
  24.     pdest = memccpy( buffer, string1, 's', 60 );
  25.     *pdest = '\0';
  26.     printf( "Result:\t\t%s\n", buffer );
  27.     printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );
  28.  
  29.     pos = pdest - buffer;
  30.     printf( "Function:\tstrcpy\n" );
  31.     printf( "Source:\t\t%s\n", string2 + pos );
  32.     pdest = strcpy( buffer + pos, string2 + pos );
  33.     printf( "Result:\t\t%s\n", buffer );
  34.     printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );
  35.  
  36.     printf( "Function:\tmemcpy 20 characters\n" );
  37.     printf( "Source:\t\t%s\n", string2 );
  38.     memcpy( buffer, string2, 20 );
  39.     printf( "Result:\t\t%s\n", buffer );
  40.     printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );
  41.  
  42.     printf( "Function:\tstrncpy 30 characters\n" );
  43.     printf( "Source:\t\t%s\n", string1 + 20 );
  44.     pdest = strncpy( buffer + 20, string1 + 20, 30 );
  45.     printf( "Result:\t\t%s\n", buffer );
  46.     printf( "Length:\t\t%d characters\n\n", strlen( buffer ) );
  47.  
  48.     getch();
  49.  
  50.     printf( "Function:\tstrdup\n" );
  51.     printf( "Source:\t\t%s\n", buffer );
  52.     newstring = strdup( buffer );
  53.     printf( "Result:\t\t%s\n", newstring );
  54.     printf( "Length:\t\t%d characters\n\n", strlen( newstring ) );
  55.  
  56.     /* Illustrate overlapping copy: memmove handles it correctly;
  57.      * memcpy does not.
  58.      */
  59.     printf( "Function:\tmemcpy with overlap\n" );
  60.     printf( "Source:\t\t%s\n", string1 + 4 );
  61.     printf( "Destination:\t%s\n", string1 + 10 );
  62.     memcpy( string1 + 10, string1 + 4, 40 );
  63.     printf( "Result:\t\t%s\n", string1 );
  64.     printf( "Length:\t\t%d characters\n\n", strlen( string1 ) );
  65.  
  66.     printf( "Function:\tmemmove with overlap\n" );
  67.     printf( "Source:\t\t%s\n", string2 + 4 );
  68.     printf( "Destination:\t%s\n", string2 + 10 );
  69.     memmove( string2 + 10, string2 + 4, 40 );
  70.     printf( "Result:\t\t%s\n", string2 );
  71.     printf( "Length:\t\t%d characters\n\n", strlen( string2 ) );
  72. }
  73.