function check = checksum ( vin )

%*****************************************************************************80
%
%% checksum() computes the checksum of a VIN.
%
%  Licensing:
%
%    This code is distributed under the MIT license. 
%
%  Modified:
%
%    09 September 2022
%
%  Input:
%
%    char vin[17]: a VIN.  
%    This is a MATLAB character array.
%
%  Output:
%
%    character check: the VIN checksum.
%    If vin is illegal, check is returned as '*'.
%
  if ( length ( vin ) ~= 17 )
    check = '*';
    return
  end

  w = [ 8, 7, 6, 5, 4, 3, 2, 10, 0, 9, 8, 7, 6, 5, 4, 3, 2 ];

  t = 0;
  for i = 1 : 17
    s = vin_ch_to_num ( vin(i) );
    t = t + s * w(i);
  end
  t = mod ( t, 11 );

  if ( isnan ( t ) )
    check = '*';
  elseif ( t == 10 )
    check = 'X';
  else
    check = char ( '0' + t );
  end

  return
end

