How to resolve the algorithm ISBN13 check digit step by step in the C programming language
Published on 7 June 2024 03:52 AM
How to resolve the algorithm ISBN13 check digit step by step in the C programming language
Table of Contents
Problem Statement
Validate the check digit of an ISBN-13 code:
You might use the following codes for testing:
Show output here, on this page
Let's start with the solution:
Step by Step solution about How to resolve the algorithm ISBN13 check digit step by step in the C programming language
The provided C code implements a function, check_isbn13
, that checks the validity of a 13-digit ISBN (International Standard Book Number) and a main function, main
, that tests the function with several ISBNs.
-
Header Files:
- The code includes the necessary standard header file,
<stdio.h>
, for input and output operations.
- The code includes the necessary standard header file,
-
Function
check_isbn13
:- This function takes a constant character array
isbn
as input and returns an integer indicating whether the ISBN is valid (1) or not (0) based on the following criteria:- ISBN must contain exactly 13 digits (excluding hyphens or spaces).
- ISBN must consist of digits only (0-9).
- ISBN must follow the specific weighted sum algorithm, where each digit (except the checksum digit) is assigned a weight based on its position. The weight alternates between 1 and 3, starting with 1 for the first digit and 3 for the second digit, and so on. The sum of the weighted digits should be divisible by 10.
- This function takes a constant character array
-
Implementation of
check_isbn13
:- It initializes the current character
ch
to the first character of theisbn
,count
to 0, andsum
to 0. - It iterates through the characters of the
isbn
until the end is reached (ch != 0
). - Within the loop:
- It skips hyphens or spaces (
ch == ' ' || ch == '-'
) in the ISBN. - It checks if the current character is a digit; if not, it returns 0, indicating an invalid ISBN.
- It calculates the weighted sum based on the position of the digit (odd positions have a weight of 3, even positions have a weight of 1).
- It skips hyphens or spaces (
- After processing all characters, it checks if the count is equal to 13, which is the expected number of digits in an ISBN.
- Finally, it checks if the
sum
is divisible by 10 (i.e.,sum % 10 == 0
). If it is, the ISBN is considered valid, and the function returns 1; otherwise, it returns 0.
- It initializes the current character
-
Function
main
:- This is the entry point of the program.
- It defines an array of four constant character arrays,
isbns
, each representing a different ISBN. - It iterates through the ISBNs, passes each one to the
check_isbn13
function, and prints whether the ISBN is valid or not.
-
Usage:
- The code demonstrates how to check the validity of ISBNs and displays the results for the provided ISBNs in
isbns
.
- The code demonstrates how to check the validity of ISBNs and displays the results for the provided ISBNs in
Source code in the c programming language
#include <stdio.h>
int check_isbn13(const char *isbn) {
int ch = *isbn, count = 0, sum = 0;
/* check isbn contains 13 digits and calculate weighted sum */
for ( ; ch != 0; ch = *++isbn, ++count) {
/* skip hyphens or spaces */
if (ch == ' ' || ch == '-') {
--count;
continue;
}
if (ch < '0' || ch > '9') {
return 0;
}
if (count & 1) {
sum += 3 * (ch - '0');
} else {
sum += ch - '0';
}
}
if (count != 13) return 0;
return !(sum%10);
}
int main() {
int i;
const char* isbns[] = {"978-1734314502", "978-1734314509", "978-1788399081", "978-1788399083"};
for (i = 0; i < 4; ++i) {
printf("%s: %s\n", isbns[i], check_isbn13(isbns[i]) ? "good" : "bad");
}
return 0;
}
You may also check:How to resolve the algorithm History variables step by step in the REXX programming language
You may also check:How to resolve the algorithm Doomsday rule step by step in the BASIC programming language
You may also check:How to resolve the algorithm Brazilian numbers step by step in the Scala programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Loops/Continue step by step in the Python programming language