home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list17_9.c < prev    next >
C/C++ Source or Header  |  1993-10-16  |  610b  |  28 lines

  1.  /* Searching for a single character with strchr(). */
  2.  
  3.  #include <stdio.h>
  4.  #include <string.h>
  5.  
  6.  main()
  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.",
  26.                  ch, loc-buf);
  27.  }
  28.