How to resolve the algorithm ISBN13 check digit step by step in the AWK programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm ISBN13 check digit step by step in the AWK 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 AWK programming language

Source code in the awk programming language

# syntax: GAWK -f ISBN13_CHECK_DIGIT.AWK
BEGIN {
    arr[++n] = "978-1734314502"
    arr[++n] = "978-1734314509"
    arr[++n] = "978-1788399081"
    arr[++n] = "978-1788399083"
    arr[++n] = "9780820424521"
    arr[++n] = "0820424528"
    for (i=1; i<=n; i++) {
      printf("%s %s\n",arr[i],isbn13(arr[i]))
    }
    exit(0)
}
function isbn13(isbn,  check_digit,i,sum) {
    gsub(/[ -]/,"",isbn)
    if (length(isbn) != 13) { return("NG length") }
    for (i=1; i<=12; i++) {
      sum += substr(isbn,i,1) * (i % 2 == 1 ? 1 : 3)
    }
    check_digit = 10 - (sum % 10)
    return(substr(isbn,13,1) == check_digit ? "OK" : sprintf("NG check digit S/B %d",check_digit))
}


  

You may also check:How to resolve the algorithm N-queens problem step by step in the F# programming language
You may also check:How to resolve the algorithm Operator precedence step by step in the 11l programming language
You may also check:How to resolve the algorithm Catalan numbers step by step in the Clojure programming language
You may also check:How to resolve the algorithm Order two numerical lists step by step in the Julia programming language
You may also check:How to resolve the algorithm Sorting algorithms/Insertion sort step by step in the Haxe programming language