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

  1. /* FINDSTR.C illustrates memory and string search functions including:
  2.  *       memchr        strchr        strrchr            strstr
  3.  */
  4.  
  5. #include <memory.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8.  
  9. int  ch = 'r';
  10. char str[] =    "lazy";
  11. char string[] = "The quick brown dog jumps over the lazy fox";
  12. char fmt1[] =   "         1         2         3         4         5";
  13. char fmt2[] =   "12345678901234567890123456789012345678901234567890";
  14.  
  15. main()
  16. {
  17.     char *pdest;
  18.     int result;
  19.  
  20.     printf( "String to be searched:\n\t\t%s\n", string );
  21.     printf( "\t\t%s\n\t\t%s\n\n", fmt1, fmt2 );
  22.  
  23.     printf( "Function:\tmemchr\n" );
  24.     printf( "Search char:\t%c\n", ch );
  25.     pdest = memchr( string, ch, sizeof( string ) );
  26.     result = pdest - string + 1;
  27.     if( pdest != NULL )
  28.         printf( "Result:\t\t%c found at position %d\n\n", ch, result );
  29.     else
  30.         printf( "Result:\t\t%c not found\n" );
  31.  
  32.     printf( "Function:\tstrchr\n" );
  33.     printf( "Search char:\t%c\n", ch );
  34.     pdest = strchr( string, ch );
  35.     result = pdest - string + 1;
  36.     if( pdest != NULL )
  37.         printf( "Result:\t\t%c found at position %d\n\n", ch, result );
  38.     else
  39.         printf( "Result:\t\t%c not found\n" );
  40.  
  41.     printf( "Function:\tstrrchr\n" );
  42.     printf( "Search char:\t%c\n", ch );
  43.     pdest = strrchr( string, ch );
  44.     result = pdest - string + 1;
  45.     if( pdest != NULL )
  46.         printf( "Result:\t\t%c found at position %d\n\n", ch, result );
  47.     else
  48.         printf( "Result:\t\t%c not found\n" );
  49.  
  50.     printf( "Function:\tstrstr\n" );
  51.     printf( "Search string:\t%s\n", str );
  52.     pdest = strstr( string, str );
  53.     result = pdest - string + 1;
  54.     if( pdest != NULL )
  55.         printf( "Result:\t\t%s found at position %d\n\n", str, result );
  56.     else
  57.         printf( "Result:\t\t%c not found\n" );
  58. }
  59.