Pure Programmer
Blue Matrix


Cluster Map

Project: Credit Card Validator

The [[Luhn Algorithm]] is a simple checksum algorithm that is used to verify that credit card numbers are correctly entered. Using this algorithm we can easily catch most single digit errors and simple transpositions.

Write a function to verify that the last digit of a credit card is the one expected when computing a checksum digit using the Luhn Algorithm. The function should take a credit card number as a string of 16 digits (no spaces) and return true or false if the last digit matches the computed checksum. Test the program with your own credit card numbers or the samples below.

Output
$ rustc CreditCardValidator.rs error[E0425]: cannot find function `strlen` in this scope --> CreditCardValidator.rs:15:21 | 15 | let length:isize = strlen(s); | ^^^^^^ not found in this scope error[E0277]: the type `str` cannot be indexed by `isize` --> CreditCardValidator.rs:20:24 | 20 | let mut c:isize = s[i] - u32('0'); | ^ string indices are ranges of `usize` | = help: the trait `SliceIndex<str>` is not implemented for `isize` = help: the trait `SliceIndex<[T]>` is implemented for `usize` = note: required for `str` to implement `Index<isize>` error[E0423]: expected function, found builtin type `u32` --> CreditCardValidator.rs:20:29 | 20 | let mut c:isize = s[i] - u32('0'); | ^^^ not a function error[E0423]: expected function, found builtin type `u32` --> CreditCardValidator.rs:33:25 | 33 | return s[length - 1] - u32('0') == checksum; | ^^^ not a function error: aborting due to 4 previous errors Some errors have detailed explanations: E0277, E0423, E0425. For more information about an error, try `rustc --explain E0277`.

Solution