home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / ISISBN.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  840b  |  46 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  ISISBN.C - Validate International Standard Book Numbers (ISBNs)
  5. **
  6. **  public domain by Maynard Hogg
  7. */
  8.  
  9. #include <ctype.h>
  10. #include "isisbn.h"
  11.  
  12. int isbn2(char *str)
  13. {
  14.       int i = 0;
  15.       int test = 0;
  16.       int c;
  17.  
  18.       while ('\0' != (c = *str++))
  19.       {
  20.             if (isdigit(c))
  21.                   c -= '0';
  22.             else if (i == 9 && 'X' == c)
  23.                   c = 10;
  24.             else continue;
  25.             test += c * ++i;
  26.       }
  27.       return (i == 10 && test % 11 == 0);
  28. }
  29.  
  30. #ifdef TEST
  31.  
  32. #include <stdio.h>
  33.  
  34. main(int argc, char *argv[])
  35. {
  36.       while (--argc)
  37.       {
  38.             int r = isbn2(*++argv);
  39.  
  40.             printf("%s is%s a valid ISBN number\n", *argv, r ? "" : " not");
  41.       }
  42.       return 0;
  43. }
  44.  
  45. #endif /* TEST */
  46.