You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
1.3 KiB
52 lines
1.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Phoneword
|
|
{
|
|
internal class PhonewordTranslator
|
|
{
|
|
public static string ToNumber(string raw)
|
|
{
|
|
if (string.IsNullOrEmpty(raw))
|
|
return null;
|
|
|
|
raw = raw.ToUpperInvariant();
|
|
|
|
var newNumber = new StringBuilder();
|
|
foreach (var c in raw)
|
|
{
|
|
if (" -0123456789".Contains(c))
|
|
newNumber.Append(c);
|
|
else
|
|
{
|
|
var result = TranslateToNumber(c);
|
|
if (result != null)
|
|
newNumber.Append(result);
|
|
else
|
|
return null;
|
|
}
|
|
}
|
|
|
|
return newNumber.ToString();
|
|
}
|
|
|
|
private static readonly string[] digits =
|
|
{
|
|
"ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ"
|
|
};
|
|
|
|
private static int? TranslateToNumber(char c)
|
|
{
|
|
for (int i = 0; i < digits.Length; i++)
|
|
{
|
|
if (digits[i].Contains(c))
|
|
return i + 2;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|