function c = isbn_check_digit_calculate ( s )

%*****************************************************************************80
%
%% isbn_check_digit_calculate() determines the check digit for an ISBN.
%
%  Discussion:
%
%    ISBN stands for International Standard Book Number.  A unique ISBN
%    is assigned to each new book.  The ISBN includes 10 digits.  There is
%    an initial digit, then a dash, then a set of digits which are a
%    code for the publisher, another digit, and then the check digit:
%
%      initial-publisher-book-check
%
%    The number of digits used for the publisher and book codes can vary,
%    but the check digit is always one digit, and the total number of
%    digits is always 10.
%
%    The check digit is interesting, because it is a way of trying to
%    make sure that an ISBN has not been incorrectly copied.  Specifically,
%    if the ISBN is correct, then its ten digits will satisfy
%
%       10 * A + 9 * B + 8 * C + 7 * D + 6 * E
%      + 5 * F * 4 * G * 3 * H + 2 * I +     J  = 0 mod 11.
%
%    Here, we've taken 'A' to represent the first digit and 'J' the
%    last (which is the check digit).  In order for the code to work,
%    the value of J must be allowed to be anything from 0 to 10.  In
%    cases where J works out to be 10, the special digit 'X' is used.
%    An 'X' digit can only occur in this last check-digit position
%    on an ISBN.
%
%  Example:
%
%    S  => 0-8493-9640-?
%    D <=  9
%
%    meaning the ISBN is 0-8493-9640-9
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    16 September 2015
%
%  Author:
%
%    John Burkardt
%
%  Input:
%
%    string S, a string.  Dashes and spaces and other
%    nonnumeric data is ignored, but at least 9 digits are expected, and only
%    the first 9 digits will be examined.
%
%  Output:
%
%    character ISBN_CHECK_DIGIT_CALCULATE, the ISBN 
%    check digit that should be appended to form the full 10 digit ISBN.
%

%
%  Extract first 9 digits.
%
  n = 9;
  dvec = s_to_digits ( s, n );
%
%  Compute the check digit.
%
  d = 0;
  for i = 1 : 9
    d = d + ( 11 - i ) * dvec(i);
  end

  d = mod ( 11 - mod ( d, 11 ), 11 );
%
%  Convert digits 0 through 9, 10 to characters '0' through '9', 'X'.
%
  c = i4_to_isbn_digit ( d );

  return
end
