3

I am making a credit card validator program in which I am asking for a string which is going to be a 16 digit number (credit card #) and I want to convert that into an int array. How do I do that? I need to then multiply every other digit starting from the first digit by 2.

char[] creditNumbers = creditCardNumber.ToCharArray();

creditNumbers[0] = (char)((int)(creditNumbers[0] * 2));
creditNumbers[2] = (char)((int)(creditNumbers[2] * 2));
creditNumbers[4] = (char)((int)(creditNumbers[4] * 2));
creditNumbers[6] = (char)((int)(creditNumbers[6] * 2));
creditNumbers[8] = (char)((int)(creditNumbers[8] * 2));

This is what I made so far but my casting is not done properly. How do I fix the problem?

svick
  • 236,525
  • 50
  • 385
  • 514
  • 3
    An example of input and desired output would really help... – Jon Skeet Oct 01 '11 at 21:28
  • 1
    First, you should be using a loop, not copying and pasting the same thing over and over. Second, try moving the parenthesis to cast to an int before multiplying, like this: `(char)(((int)creditNumbers[0]) * 2)` – someone Oct 01 '11 at 21:29
  • 5
    What happens if the digit represents a numerical value `>= 5` and therefore doubling its numerical value doesn't result in a single-digit number? What on earth are you doing to the poor credit cards anyway? – jason Oct 01 '11 at 21:29
  • This may help: http://stackoverflow.com/questions/3665757/c-convert-char-to-int – drneel Oct 01 '11 at 21:31
  • @Jason looks like he's doing a checksum calculation – Daniel Mann Oct 01 '11 at 21:34
  • It's simple. Really simple. First you have to convert the string into an integer variable, and then use the Algorithm for turning a number into digits. The algorithm is one of the first things I have learned in school. Search around the Net to see what I'm talking about. In order to make it universal (to work with N digit numbers) you will have to use loops. – AlexSavAlexandrov Oct 01 '11 at 21:42
  • @HansPassant, why do you think this is a homework? – svick Oct 01 '11 at 21:54
  • @DBM: Maybe, but converting back to characters is completely misguided. – jason Oct 01 '11 at 22:01
  • @svick - because validating *my credit card number* is not left up to programmers that don't know how to parse it? Wishful thinking perhaps, I sure hope not. Kudos to teachers being great at giving out-of-the-box assignments, it always shows. – Hans Passant Oct 01 '11 at 22:08

2 Answers2

8

To just get the int array, I would do this:

creditCardNumber.Select(c => int.Parse(c.ToString())).ToArray()

To check the number using the Luhn algorithm, you could do this:

bool IsValid(string creditCardNumber)
{
    var sum = creditCardNumber.Reverse()
                              .Select(TransformDigit)
                              .Sum();

    return sum % 10 == 0;
}

int TransformDigit(char digitChar, int position)
{
    int digit = int.Parse(digitChar.ToString());
    if (position % 2 == 1)
        digit *= 2;

    return digit % 10 + digit / 10;
}
svick
  • 236,525
  • 50
  • 385
  • 514
2
var intArray = creditCardNumber.ToCharArray().Select(o => int.Parse(o.ToString())).ToArray();
var result = new List<int>();

for(int i=0; i<intArray.Length; i++){

    if((i % 2) == 0)
      result.Add(intArray[i] * 2);
    else
        result.Add(intArray[i]);
}

Console.Write(string.Concat(result.Select(o => o.ToString())));
svick
  • 236,525
  • 50
  • 385
  • 514
terjetyl
  • 9,497
  • 4
  • 54
  • 72