public override string Scramble(string validNumber) { Contract.Requires(IsValid(validNumber)); string rot5number = Rot.Rot5(validNumber); int lastDigitPosition = rot5number.LastIndexOfAny(Chars.Digits); // aaaaaaa1bb // 0123456789 // length = 10 // lastDigitPosition = 7 // suffixLength = 2 // prefixLength = 7 string incompleteNumber = rot5number.Substring(0, lastDigitPosition); string result; try { result = Complete(incompleteNumber); } catch (Exception) { // checkDigit was 10, so force it to be 8: // we alter the 2nd digit from the end; the modulus was 10, the total will be 1*20+10 resulting in a modulus of 8 int alterDigitPosition = incompleteNumber.LastIndexOfAny(Chars.Digits); char alterDigitChar = incompleteNumber[alterDigitPosition]; int alterDigit = int.Parse(alterDigitChar.ToString()); int alteredDigit = (alterDigit + 1) % 10; string alteredIncompleteNumber = incompleteNumber.Substring(0, alterDigitPosition) + alteredDigit + incompleteNumber.Substring(alterDigitPosition + 1); result = Complete(alteredIncompleteNumber); } result += rot5number.Substring(lastDigitPosition + 1); // make sure non digits get at the end return(result); }