Trying to implement a luhn algorithm but with a spaced string as an input. I've tried Linq, some predefined string functions that might be directly related to it (Trim(), Replace()) but it dosen't print out the correct answer
Here's my code:
static bool checkCard(string cardNo)
{
int[] cardInt = new int[cardNo.Length];
for(int i = 0;i < cardNo.Length; i++){
cardInt[i] = (int)(cardNo[i]);
}
for(int i = cardNo.Length - 2;i >= 0;i-=2){
int temporaryValue = cardInt[i];
temporaryValue*=2;
if(temporaryValue > 9){
temporaryValue = temporaryValue % 10 +1;
}
cardInt[i] = temporaryValue;
}
int sum = 0;
for(int i = 0;i<cardNo.Length;i++){
sum+=cardInt[i];
}
if(sum % 10 == 0){
return true;
}
return false;
}
static void Main(string[] args)
{
int n = int.Parse(Console.ReadLine());
for (int i = 0; i < n; i++)
{
string card = Console.ReadLine();
if (checkCard(card))
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
Input : 4556 7375 8689 9855
The output must be "YES" But it prints "NO" instead What can I edit so I get rid of this error?