Click here to Skip to main content
15,891,316 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I am trying to read a string which should consist of eight characters, so I can add up the digits and compute the check digit of an ISSN, according to the article described below.

So far the problem is to find a way to count the contents of the array i.e.
ISSN=0378-5955
i.e.
1. add the numeric value of each number for the first seven digits
2. modulus of (1)/11 calculated
if there is no remainder the check digit is 0, otherwise the remainder value is subtracted from 11 to give the check digit

Source:ISSN

public void setISSN(String ISSN)
  {
      String[] ISSNArray = ISSN.Split();
      int [] ISSNSequence = { };
      int SumOfFIrstSevenDigits = 0;
      for (int i = 0; i <= ISSNArray.Length - 1; i++)
      {

      }
  }
Posted
Comments
Sergey Alexandrovich Kryukov 12-Nov-13 13:24pm    
What's the problem? I cannot see where you try to read anything from string.
—SA

C#
int lastDigit = 0;
// Unicode guarantees the digits are in order and consecutive values
int sum = ISSN.Sum(c => char.IsDigit(c) ? lastDigit = (int)c - (int)'0' : 0);
sum -= lastDigit;
int mod = sum % 11;
int checkDigit = 0;
if (mod != 0)
  checkDigit = 11 - mod;

if (checkDigit == lastDigit)
{
  // true if the lastDigit IS the correct checkDigit
}

The example you gave (0378-5955) appears to be of an invalid ISSN.
The check digit should be 4.

[Edit: since you didn't describe the algorithm correctly, the above didn't do the right thing, so I'll fix it.]

C#
int lastDigit = 0;
int digitPosition = 8;
int sum = ISSN.Sum(c => {
  if (!char.IsDigit(c))
    return 0;
  lastDigit = (int)c - (int)'0';  // Unicode guarantees the digits are in order and consecutive values
  return digitPosition-- * lastDigit;
});
if (digitPosition != 0)
  throw new ArgumentException("Incorrect number of digits in ISSN");
sum -= lastDigit;
int mod = sum % 11;
int checkDigit = 0;
if (mod != 0)
  checkDigit = 11 - mod;

if (checkDigit == lastDigit)
{
  // true if the lastDigit IS the correct checkDigit
}
 
Share this answer
 
v2
public void setISSN(String ISSN)
   {
       // validate ISSN
       int[] d = new int[ISSN.Length];
       int c1 = 0; int c2 = 0; int c3 = 0;
       for (int i = 0; i <= ISSN.Length; i++)
       {
           if (i <= 6)
           {
               d[i] = int.Parse(ISSN[i].ToString());
               c1 += d[i];
           }

           c2 = c1 % 11;
           c3 = 0;
           if (c2 == 0) { c3 = 0; }
           else { c3 = 11 - c2; }

           /* Sourced from http://en.wikipedia.org/wiki/International_Standard_Serial_Number,
            * last updated on 23rd September 2013 */
       }
       if (ISSN[6] != c3) throw new System.ArgumentException("ISSN may be invalid", "ISSN Validation");
   }
 
Share this answer
 
Comments
Matt T Heffron 12-Nov-13 14:33pm    
This implementation is incorrect and inefficient.
It isn't multiplying by the digit position while computing the sum.
The d array appears totally unnecessary.
int.Parse(ISSN[i].ToString()) is a very inefficient way to convert a digit to the number it represents.
You have the modulo arithmetic and check digit calculation INSIDE the summation loop.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900