In: Computer Science
Write an ISBN Validator in C language
What are ISBN?
ISBN, or International Standard Book Number, is a numeric identifier for commercially printed books. Prior to 2007, they had 10 digits, but all newly issued ISBN use 13 digits. They are used to uniquely identify a book and consist of several portions. These include the registrant group, publisher, title, and a check digit. The registrant group indicates where the publisher is located. For example a registrant group of 0 or 1 indicates an English speaking area, while 960 indicates Greece, and 99936 indicates Bhutan. The publisher and title are codes issued by various country specific groups. In the US this is R.R. Bowker. Note that the length of the registrant group, title and publisher are not of a fixed width which gives flexibility to the issuers to allocate more numbers to higher demand groups. For instance there are more books published in English than in Greek, thus the 0/1 registrant group for English and 960 for Greek. In addition, the final digit of an ISBN is a check digit. This digit is calculated from the others and helps identify valid ISBN versus ones that may have a typo.
Customarily, ISBN are specified with hyphens or spaces separating the various groups, but these are aesthetic only. They do not affect the calculation of the check digit. However, to simplify reading the ISBN, you only need to handle hyphens.
ISBN-10
ISBN-10 have 9 characters specifying the groups detailed above and one 1 character for a check digit. The 9 group characters are all digits ranging from 0 through 9. The check digit is base 11, so it is represented as 0-9 with 10 represented as an X.
The check digit satisfies the following equation
10x1+9x2+8x3+7x4+6x5+5x6+4x7+3x8+2x9+x10≡0(mod11)10x1+9x2+8x3+7x4+6x5+5x6+4x7+3x8+2x9+x10≡0(mod11)
where x10x10 is the check digit. Note the relationship between the coefficient and the digit (look at their sum).
Note there can be only one X in a valid ISBN-10, in the position of the checksum digit.
ISBN-13
ISBN-13 have 13 characters. 12 of them specify the groups as detailed above with the final character a check digit. Unlike in ISBN-10, the check digit of ISBN-13 is always one of 0 through 9.
The check digit satisfies the following equation:
x1+3x2+x3+3x4+x5+3x6+x7+3x8+x9+3x10+x11+3x12+x13≡0(mod10)x1+3x2+x3+3x4+x5+3x6+x7+3x8+x9+3x10+x11+3x12+x13≡0(mod10)
where x13x13 is the check digit. Note the relationship between the coefficient and the digit read.
Note that a valid ISBN-13 does not have any X in them.
Your Program
Your program should perform the following actions:
Example Executions
User input is in bold
Please enter an ISBN: 0-486-63760-3 The input '0-486-63760-3' is: Valid ISBN-10 Invalid ISBN-13
Please enter an ISBN: 0-521-33781-X The input '0-521-33781-X' is: Valid ISBN-10 Invalid ISBN-13
Please enter an ISBN: 978-3-642-43001-5 The input '978-3-642-43001-5' is: Invalid ISBN-10 Valid ISBN-13
Please enter an ISBN: 01234567X5 X can only occur as a check digit The input '01234567X5' is: Invalid ISBN-10 Invalid ISBN-13
Please enter an ISBN: 0123456789 The input '0123456789' is: Valid ISBN-10 Invalid ISBN-13
Please enter an ISBN: 012345678 The input '012345678' is: Invalid ISBN-10 Invalid ISBN-13
Please enter an ISBN: 012345678905X The input '012345678905X' is: Invalid ISBN-10 Invalid ISBN-13
Please enter an ISBN: 01234569XX Invalid number of Xs The input '01234569XX' is: Invalid ISBN-10 Invalid ISBN-13
Additional Tests
The following are all valid ISBN.
0-486-63760-3 981-02-4897-0 3-540-20105-X 0-521-33781-X 0-521-33781-x 978-0-486-63760-0 978-0-387-51768-1 978-3-642-43001-5 978-981-02-4897-0 0123456789 0123456789012
The following are all not valid ISBN.
012345678 01234567890 012345678901 0123456789013 01234567890X5 012345678905X 01234567X5 0123456XX7 01234569XX
You should also look around at books you have an verify the ISBN they have on the back work. However, it is possible for there to be errors, so double check they are good at another ISBN validator (Links to an external site.).
Hints
#include <ctype.h> // .... char c; // ... something fills in a value for c if (isdigit(c)) { printf("c is a digit\n"); }
value = c - '0';and if it is an X, then the value is just 10.
[Note that some the data provided for testing are actually invalid ISBNs. Please do manually check before flagging the program as erroneous]
PROGRAM
//program to validate ISBN Numbers
//include header files
#include<stdio.h>
#include<ctype.h>
//main function
int main(void)
{
//declaring variables
int sum = 0,//hold sum of isbn numbers
digit, //extract a digit from isbn
digitcount, //count the digits in isbn
i, //temporary loop variable
xflag=0, //check the presence of character x in isbn
xcount=0, //count occurrence of x
invalidcharflag; //hold status of invalid character in isbn
char isbn[20], //hold isbn
c; //temprary variable to hold repeat choice
//Repeat till user quits
do{
//read isbn
printf("Enter an ISBN: ");
scanf("%[^\n]",isbn);
//set flags
invalidcharflag=0;
xflag=0;
digitcount=0;
//analyze isbn for validity of isbn10
for (i = 0; isbn[i]!='\0'; i++)
{
//check for invalid characters
if(!(isdigit(isbn[i]) || isbn[i]=='-'||toupper(isbn[i]=='X')))
{
invalidcharflag=1;
break;
}
//check and process x
if(toupper(isbn[i])=='X')
{
digit=10;
xflag=1;
xcount++;
}
else
digit=isbn[i]-'0';
//calculate sum
if(digit>=0 && digit<=10)
sum += (digit * (10 - i));
if(isbn[i] != '-') digitcount++;
}
//process if isbn is a valid isbn
printf("\nThe input %s is :",isbn);
if(invalidcharflag==1)
printf("\nInvalid ISBN10");
else if((xflag ==1 && toupper(isbn[i-1])!='X') || xcount>1)
printf("\nInvalid ISBN10");
else if(digitcount!=10)
printf("\nInvalid ISBN10");
else if(sum%11==0)
printf("\nValid ISBN10");
//reset flags for testing for isbn13
invalidcharflag=0;
digitcount=0;
sum=0;
//processing for isbn13
for (i = 0; isbn[i]!='\0'; i++)
{
//check for invalid characters
if(!(isdigit(isbn[i]) || isbn[i]=='-'))
{
invalidcharflag=1;
break;
}
//calculate sum
digit=isbn[i]-'0';
if(isbn[i]!='-')
{
if((i+1)%2==1)
sum += digit;
else
sum += (digit*3);
digitcount++;
}
}
//check if the given isbn is a valid isbn13
if(invalidcharflag==1)
printf("\nInvalid ISBN13");
else if(digitcount!=13)
printf("\nInvalid ISBN13");
else if(sum%10==0)
printf("\nValid ISBN13");
//printf("sum=%d",sum);
printf("\n\nCheck another ISBN (Y/N): ");
scanf("%*c%c%*c",&c);
}
while(toupper(c)=='Y');
return 0;
}
SCREENSHOT OF PROGRAM
OUTPUT OF PROGRAM
Enter an ISBN: 0-486-63760-3
The input 0-486-63760-3 is :
Valid ISBN10
Invalid ISBN13
Check another ISBN (Y/N): y
Enter an ISBN: 0-521-33781-X
The input 0-521-33781-X is :
Valid ISBN10
Invalid ISBN13
Check another ISBN (Y/N): y
Enter an ISBN: 978-3-642-43001-5
The input 978-3-642-43001-5 is :
Invalid ISBN10
Valid ISBN13
Check another ISBN (Y/N): y
Enter an ISBN: 01234567X5
The input 01234567X5 is :
Invalid ISBN10
Invalid ISBN13
Check another ISBN (Y/N): y
Enter an ISBN: 0123456789
The input 0123456789 is :
Invalid ISBN10
Invalid ISBN13
Enter an ISBN: 012345678
The input 012345678 is :
Invalid ISBN10
Invalid ISBN13
Check another ISBN (Y/N): y
Enter an ISBN: 012345678905X
The input 012345678905X is :
Invalid ISBN10
Invalid ISBN13
Check another ISBN (Y/N): y
Enter an ISBN: 01234569XX
The input 01234569XX is :
Invalid ISBN10
Invalid ISBN13
Check another ISBN (Y/N): y
Enter an ISBN: 0-486-63760-3
The input 0-486-63760-3 is :
Invalid ISBN10
Invalid ISBN13
Enter an ISBN: 3-540-20105-X
The input 3-540-20105-X is :
Valid ISBN10
Invalid ISBN13
Check another ISBN (Y/N): y
Enter an ISBN: 0-521-33781-X
The input 0-521-33781-X is :
Invalid ISBN10
Invalid ISBN13