-2

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?

SalimKazi
  • 49
  • 1
  • 9
  • `(int)(cardNo[i])` is not digit ... it's ascii/char value so for example `'1' ` is `49` – Selvin Jul 08 '21 at 12:11
  • https://learn.microsoft.com/de-de/dotnet/api/system.char.isdigit?view=net-5.0 to check if its a digit and if you want the int value you'll either need to convert to string first or use int.Parse – T.Schwarz Jul 08 '21 at 12:11
  • 3
    What about `cardNo = cardNo.Replace(" ", "");`? (Assuming you want to get rid of every spaces, and spaces can be anywhere) – Rafalon Jul 08 '21 at 12:14
  • Does this answer your question? [Efficient way to remove ALL whitespace from String?](https://stackoverflow.com/questions/6219454/efficient-way-to-remove-all-whitespace-from-string) – gunr2171 Jul 08 '21 at 12:15
  • @gunr2171, nope, still having the same problem – SalimKazi Jul 08 '21 at 12:35
  • What others said is that *casting* - `(int)c` - is different from *parsing* - `int.Parse(c)`. Here is why your result is different from what you expected. – Rafalon Jul 08 '21 at 13:40
  • @Rafalon, you get cs1503 error there – SalimKazi Jul 08 '21 at 13:44
  • `int.Parse(c.ToString())` if you prefer, whatever – Rafalon Jul 08 '21 at 13:46

2 Answers2

0

In your case i don't see why you can't just use char.IsDigit() seeing as credit card numbers aren't alphanumeric here a short linq example:

        var digitsOnly = cardNo.Where(x => char.IsDigit(x)).ToArray();
        int[] integersArray = digitsOnly.Select( x = int.Parse(x.ToString())).ToArray();  

btw if you cast char to int you'll get its ASCII intvalue so 0 would be 48, 1 is 49 etc.

T.Schwarz
  • 130
  • 6
0

the right answer to this problem was proposed by @Rafalon

Adding a Replace(" ", "") function after every string reading

SalimKazi
  • 49
  • 1
  • 9