Validate PIN code programming exercise.
Pin code validation emergency!!! Coders needed!
ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.
Write a function that returns true if a given string is possible PIN code or false if not.
Example:
"1234" => true
"123456" => true
"12ab" => false
You can scroll down for the solution. Or not. It is a good solution so you should scroll down. Just saying. Came on, PLEASE 🙂 🙂
public static bool ValidatePin(string pin) { //again there is many ways do solve this coding problem //I just want to introduce the TryParse() function //So first we check if the length of the string is 4 or 6 //with the ||(OR) operator. //And then, about TryParse(): //this function does not one but two things. //It is not very usual for functions to do that. //The way it does it, is with this "out" keyword. //You can google it :) //1) converts string to a number //2) returns true or false for the success of the operation //The rest is simple //if the (the Length of the string) Length=4 or Length=6 //and if the pin is successfully converted to a number //we return true. Else we return false. if (pin.Length==4 || pin.Length ==6) { int r; if (int.TryParse(pin, out r)) return true; else return false; } else { return false; } }