home *** CD-ROM | disk | FTP | other *** search
/ Sams Teach Yourself C in 21 Days (6th Edition) / STYC216E.ISO / mac / Examples / Day17 / strchr.c / strchr.c
Encoding:
C/C++ Source or Header  |  2002-08-11  |  619 b   |  30 lines  |  [TEXT/LMAN]

  1. /* Searching for a single character with strchr(). */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5.  
  6. int main( void )
  7. {
  8.     char *loc, buf[80];
  9.     int ch;
  10.  
  11.     /* Input the string and the character. */
  12.  
  13.     printf("Enter the string to be searched: ");
  14.     gets(buf);
  15.     printf("Enter the character to search for: ");
  16.     ch = getchar();
  17.  
  18.     /* Perform the search. */
  19.  
  20.     loc = strchr(buf, ch);
  21.  
  22.     if ( loc == NULL )
  23.         printf("The character %c was not found.", ch);
  24.     else
  25.         printf("The character %c was found at position %d.\n",
  26.                 ch, loc-buf);
  27.     return 0;
  28. }
  29.  
  30.