Question

In: Computer Science

Write an ISBN Validator in C language What are ISBN? ISBN, or International Standard Book Number,...

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:

  1. Prompt the user to enter an ISBN. Your program should read this in as a C-string (NUL-terminated character array).
  2. Loop through the string character by character, stopping when you see a NUL character ('\0').
    As you go, update the calculation of the ISBN-10 and ISBN13 checks.
  3. At the end of the loop, if you have
    1. read the correct number of digits,
    2. and the checksum is zero,
    3. and if an X appears, it is only in the check digit of an ISBN-10
    then you have a valid ISBN.
  4. Print out your result

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

  • The function isdigit() (Links to an external site.) defined in <ctype.h> is helpful when processing the characters. It is used like
    #include <ctype.h>
    // ....
    char c;
    // ... something fills in a value for c
    if (isdigit(c)) {
      printf("c is a digit\n");
    }
  • When reading your string, if you see anything other than a digit, the letter X, or a hyphen, that makes the ISBN invalid.
  • The X in an ISBN could be either lower case or upper case. Make sure to handle both.
  • Remember to translate a character from ASCII to an integer when calculating the checksum. If the character is a digit, this can be done by
    value = c - '0';
    and if it is an X, then the value is just 10.
  • As you loop through the string, it is useful to track the number of digits read. This can be used to calculate the coefficients for the checksum calculations.
  • As you loop through the string, it is useful to track the number of Xs you encounter and the last digit you read. This is useful to know if you have read multiple Xs (and thus invalidate the ISBN) and to verify only the last digit read, the checksum, could be an X. Remember only ISBN-10 allow X, so any Xs invalidate ISBN-13s

Solutions

Expert Solution

[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


Related Solutions

CIS- Python (Business: check ISBN-10) An ISBN-10 (International Standard Book Number) consists of 10 digits: d1d2d3d4d5d6d7d8d9d10....
CIS- Python (Business: check ISBN-10) An ISBN-10 (International Standard Book Number) consists of 10 digits: d1d2d3d4d5d6d7d8d9d10. The last digit, d10, is a checksum, which is calculated from the other nine digits using the following formula: (d1 * 1 + d2 * 2 + d3 * 3 + d4 * 4 + d5 * 5 + d6 * 6 + d7 * 7 + d8 * 8 + d9 * 9) % 11 If the checksum is 10, the last digit...
Write a C program for a library automation which gets the ISBN number, name, author and...
Write a C program for a library automation which gets the ISBN number, name, author and publication year of the books in the library. The status will be filled by the program as follows: if publication year before 1985 the status is reference else status is available. The information about the books should be stored inside a linked list. The program should have a menu and the user inserts, displays, and deletes the elements from the menu by selecting options....
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in...
C LANGUAGE ONLY Write a C program to count the total number of duplicate elements in an array. Enter the number of elements to be stored in the array: 3 Input 3 elements in the arrangement: element [0]: 5 element [1]: 1 element [2]: 1 Expected output: The total number of duplicate elements found in the array is: 1
In C language: Write a function which can receive a float array, an integer number(number of...
In C language: Write a function which can receive a float array, an integer number(number of elements) as input arguments and print all the elements in the array with 2 decimal precision.
The most common attributes of a book are the Book Title, and ISBN. The most common...
The most common attributes of a book are the Book Title, and ISBN. The most common functions are to set the Book Title, and ISBN, Write the code to implement this problem. 1. Write the UML Diagram that represents this class Book 2. Use code blocks editor and in C++ Write a header file Book with these properties. Write the implementation file for the member functions.
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is...
Write a RIMS-compatible C-language for-loop that counts the number of times a bit of A is followed by a bit of the opposite parity (01 or 10) and writes the value to B. For example 00100110 has 4 cases: 00100110, 00100110, 00100110, 00100110.
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
Teen Book Review: The grieving teen: A guide for teenagers and their frends. ISBN: 0684868040 ISBN-13:...
Teen Book Review: The grieving teen: A guide for teenagers and their frends. ISBN: 0684868040 ISBN-13: 9780684868042 Authors:Helen FitzGerald THE FOLLOWING QUESTIONS ARE NOT CONTENT FROM THE BOOK BUT THOUGHTS FROM THE BOOK Recently many publishers have responded to the need for books that are designed to help adults discuss a difficult topic, death, with young children, and/or teens. You are to select one book (many are on Amazon), written for teens, and evaluate it based on what we have...
In C language: Write the fnFindMin function which can receive a float array, an integer number(number...
In C language: Write the fnFindMin function which can receive a float array, an integer number(number of elements) as input arguments and finds the minimum value in the array. This function should return the array index (subscript) of the minimum value.
C++ Write a definition for a structure type for records for books. The record contains ISBN...
C++ Write a definition for a structure type for records for books. The record contains ISBN number, price, and book cover (use H for Hard cover and P for paper cover). Part of the problem is appropriate choices of type and member names. Then declare two variables of the structure. Write a function called GetData use the structure as function argument to ask the user to input the value from the keyboard for structure variables.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT