search.c
Upload User: bjtelijie
Upload Date: 2010-01-01
Package Size: 87k
Code Size: 1k
Category:

Algorithm

Development Platform:

Visual C++

  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. # include <ctype.h>
  4. char *alpha = "abcdefghijklmnopqrstuvwxyz";
  5. int comp(const void *ch, const void *s);
  6. int main()
  7. {
  8. char ch;
  9. char *p;
  10. printf("Enter a character: ");
  11. ch = getchar();
  12. ch = tolower(ch);    /* 将变元ch转换成小写字符 */
  13. p = (char *)bsearch(&ch, alpha, 26, 1, comp);
  14. if(p)
  15. printf("%c is in alphabetn", *p);
  16. else
  17. printf("is not in alphabetn");
  18. return 0;
  19. }
  20. int comp(const void *ch, const void *s)
  21. {
  22. return *(char *)ch -*(char *)s;
  23. }