using System.Text.RegularExpressions;
public static void Main()
bool est = IsRutOk("18767087K");
Console.WriteLine(est ? "true" : "false");
public static bool IsRutOk(string rut)
const string RutRegex = "[0-9]+K?";
Regex RegExRut = new Regex(RutRegex, RegexOptions.Compiled |
RegexOptions.IgnoreCase);
int[] coefs = {3, 2, 7, 6, 5, 4, 3, 2};
rut = rut.Trim().ToUpperInvariant();
if (!RegExRut.IsMatch(rut)) { return false; }
if (rut.Length > 9) { return false; }
while (rut.Length < 9) { rut = "0" + rut; }
for (int index = 0; index < rut.Length - 1; index++)
char curr = rut.Substring(index, 1).ToCharArray()[0];
total += coefs[index]*(curr - '0');
int rest = 11 - (total%11);
if (rest == 11) rest = 0;
if ((rest == 10) && rut.EndsWith("K")) { return true; }
if (rut.Substring(rut.Length - 1, 1).ToCharArray()[0] == ('0' + rest))