Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- string eng = "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>`~ёЁ";
- string ru = "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮеЕеЕ";
- for (int i = 0; i < eng.Length; ++i)
- if (query.Contains(eng[i]))
- query = query.Replace(eng[i], ru[i]);
- public sealed class Replacer
- {
- private readonly Dictionary<Char, Char> _dictionary;
- public Replacer(String sourceSymbols, String targetSymbols)
- {
- if (sourceSymbols.Length != targetSymbols.Length)
- throw new NotSupportedException("sourceSymbols.Length != targetSymbols.Length");
- Int32 count = sourceSymbols.Length;
- Dictionary<Char, Char> dictionary = new Dictionary<Char, Char>(count);
- for (int i = 0; i < count; i++)
- dictionary.Add(sourceSymbols[i], targetSymbols[i]);
- _dictionary = dictionary;
- }
- public void FixCharacters(ref String query)
- {
- if (String.IsNullOrEmpty(query))
- return;
- unsafe
- {
- Int32 index = query.Length - 1;
- fixed (Char* chPtr = query)
- {
- while (index >= 0)
- {
- Char oldChar = chPtr[index];
- Char newChar;
- if (_dictionary.TryGetValue(oldChar, out newChar))
- chPtr[index] = newChar;
- index--;
- }
- }
- }
- }
- }
- static void Main(string[] args)
- {
- String eng = "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>`~ёЁ";
- String rus = "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮеЕеЕ";
- Replacer replacer = new Replacer(eng, rus);
- for (int i = 0; i < 10; i++)
- {
- String query = $"Hello World {i}";
- replacer.FixCharacters(ref query);
- Console.WriteLine(query); // "Руддщ Цщкдв"
- }
- }
- public static class LangConversion
- {
- private static readonly Dictionary<char, char> engToRu = new Dictionary<char, char>();
- static LangConversion()
- {
- var eng = "qwertyuiop[]asdfghjkl;'zxcvbnm,.QWERTYUIOP{}ASDFGHJKL:"ZXCVBNM<>`~ёЁ";
- var ru = "йцукенгшщзхъфывапролджэячсмитьбюЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮеЕеЕ";
- for (var i = 0; i < eng.Length; i++)
- engToRu[eng[i]] = ru[i];
- }
- public static string Fix(string str)
- {
- var sb = new StringBuilder();
- foreach (char c in str)
- {
- char fixedChar;
- sb.Append(engToRu.TryGetValue(c, out fixedChar) ? fixedChar : c);
- }
- return sb.ToString();
- }
- }
- Исходный вариант: 6480мс
- Dictionary: 2330мс
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement