# Processing a book ISBN

This is a discussion on Processing a book ISBN within the C Programming forums, part of the General Programming Boards category; Hi, I am trying to figure out this program. It accepts an ISBN (International Standard Book Number) from the user, ...

1. ## Processing a book ISBN

Hi,
I am trying to figure out this program. It accepts an ISBN (International Standard Book Number) from the user, and then decides whether it was entered correctly or not based on the formula used in calculating an ISBN. An ISBN is a 10 digit number that uses the last digit as error correction. So if the ISBN is 0132261197, its calculated as:
1*0 + 2*1 + 3*3 + 4*2 + 5*2 + 6*6 + 7*1 + 8*1 + 9*9 = 161. The last digit 7, which is the check digit, is the remainder when 161 is divided by 11. So if the numbers are entered incorrectly, the remainder would not match the check digit, causing the program to report an incorrect ISBN.

I have the program working up until the check digit is found in the string and assigned to a variable for later comparison. What I can't get right is the way that the math is done here. The book says to store the ISBN in a character array, then do the summation using the fact that the numeric value of a digit character can be had by subtracting the character '0' from the character. Like '4' - '0' is 4. I don't understand how to set this up for the string. Anyway, here is what I have so far.
I know I shouldn't use gets, but I haven't learned how to use fgets yet.
Code:
```#include <stdio.h>

main()

{
char  isbn[52],
*isbn_ptr,
check_digit,
title[52];            /*book title*/

gets(title);

gets(isbn);

for(isbn_ptr = isbn; *isbn_ptr != '\0'; ++isbn_ptr)
;
/*goes to end of string and finds check digit*/
--isbn_ptr;
check_digit = *isbn_ptr;

}```
Any help would be appreciated.

2. I'd suggest reading this and this, as well as your text, until it makes sense. You could submit something like this, but who knows if your instructor might ask if you can explain it.
Code:
```int valid_isbn(const char *text)
{
int i, sum = 0;
for (i = 0; i < 9; ++i)
{
sum += (text[i] - '0') * (i + 1);
}
return sum % 11 == text[i] - '0';
}```

3. ## Search this board....

Also, try searching this board.

I seem to recall a post about ISBN parsing posted to the board within the last year.....

4. I pretty much understand that whole thing, but how does that return statement work: The remainder of sum / 11 is equal to text[] - '0' ? If we used 0132261197 for the ISBN, Text[] would equal 9 at the end of the loop. So how would that equal the remainder of sum / 11?

5. 161 / 11 = 14
14 * 11 = 154
161 - 154 = 7

Quzah.

6. That's all true, but how is text[] ever going to equal the remainder? Is there a condition that has to be met? I don't know how the return statement is read.

7. Code:
`return ((sum % 11) == (text[i] - '0'));`
This means 0 is returned if an error is found.

8. Why is text[i] == 9? It == 7. The loop will increment i to the last digit (ie the check digit), check the for() conditional, find that i is 9, and exit. Still, you can clear things up by writing instead

Code:
`return sum % 11 == text[9] - '0';`

And of course, loop unrolling

Code:
```int valid_isbn(const char *isbn)
{
return (isbn[0] + isbn[1]*2 + isbn[2]*3 + isbn[3]*4 + isbn[4]*5
+ isbn[5]*6 + isbn[6]*7 + isbn[7]*8 + isbn[8]*9 - '0') % 11
== ((isbn[9]=='X'||isbn[9]=='x')?10:(isbn[9]-'0'));
}```
Where X/x is 10 in base 11, of course. You missed it Dave

9. Too bad your unrolled loop is wrong. You're multiplying the value of each character. See, when you're trying to be clever, it actually helps if you are.

Quzah.

Prove it.

11. What is 1 * 2 + 3 * 3?
What is 49 * 2 + 51 * 3 - 48?
Are the two equal?
If I add a number to a total ten times, then subtract it once, is that the same thing as adding it ten times and subtracting it ten times?

Quzah.

12. Originally Posted by jafet
Still, you can clear things up by writing instead
Code:
`return sum % 11 == text[9] - '0';`
I wasn't trying to be clear.

13. Originally Posted by richdb
If we used 0132261197 for the ISBN, Text[] would equal 9 at the end of the loop. So how would that equal the remainder of sum / 11?
I thought then you were just trying to make me think.

14. Alright, I was trying to be clever. But the code is correct, if not intuitive. Remember that we are subtracting '0' a total of 45 times from the LHS, and 44n % 11 == 0. So we only need to subtract '0' once.