0

Sorry for my bad english. So, i have a code.

    public Random rnd = new Random();

    public string RandomizeLetters(string text)
    {
        string variations = "";

        foreach (var letter in text)
        {
            var а = new List<string> { "а", "аa" };
            var е = new List<string> { "е", "еe" };
            var о = new List<string> { "о", "оo" };
            var р = new List<string> { "р", "рp" };
            var с = new List<string> { "с", "сc" };
            var у = new List<string> { "у", "уy" };
            var х = new List<string> { "х", "хx" };

            var alphavite = new List<List<string>> { а, е, о, р, с, у, х };

            var res = new StringBuilder();

            foreach (var x in alphavite)
            {
                if (x.Exists(e => e.EndsWith(letter.ToString())))
                {
                    res.Append(x[1][rnd.Next(0, x[1].Length)]);
                }
            }

            res.Append(letter.ToString());

            variations += res;
        }

        return variations.ToString();
    }

This code replaces random letters with letters that are visually similar to letters from a different keyboard layout, in my case, from Russian keyboard layout to the English keyboard layout. But instead of the desired result, I get text with duplicate characters.

Here is the text I want to process:

Для проверки наличия в вашем тексте символов из другого языка - скопируйте исходный текст, вставьте его в поле ниже, и выберите нужный чекбокс языка

But the text that I get at the output:

Для пpроовeеpрки нaаличия в ваашeем теекcстeе cсимвоолоов из дрруугоогоо языкaа - сскоопиpрууйтее иcсxхoодный теекcст, всстaавьтее eегoо в поолeе нижее, и выбeеpритeе нyужный чеекбоокcс языкаа

How to fix it? I check the text with the help of the service for checking the characters: raskladka.obmen-service.com

Behavior
  • 41
  • 8
  • `var а = new List { "а", "аa" };` wtf? What are you even trying to do with that code, I seriously can't tell. – EpicKip Jul 10 '17 at 12:27
  • @EpicKip letter "a" from English keyboard layout, but letter "а" from Russian keyboard layout. These are different letters. And I want to replace Russian characters with English ones in the text. – Behavior Jul 10 '17 at 12:30
  • @SergeyBerezovskiy res.Append(x[1][rnd.Next(0, x[1].Length)]) This code selects a random character from two possible – Behavior Jul 10 '17 at 12:32
  • You always append original letter `res.Append(letter.ToString());`, even if before that you appended modified letter – Evk Jul 10 '17 at 12:34
  • @Behavior Still doesn't make sense to make a list for each possible letter and also you put this: `{ "а", "аa" }` in there.. what did you expect? 1 a will be replaced by 1 a. If you replace it by 2 you get a duplicate -_- – EpicKip Jul 10 '17 at 12:36
  • @Evk This is a temporary code. I just do not understand how to make the characters that are in the collection replace, and if there is no such symbol in the collection, then the original character was added to the text. – Behavior Jul 10 '17 at 12:36
  • @EpicKip res.Append(x[1][rnd.Next(0, x[1].Length)]) I wrote above about this code, it does not create duplicates, it chooses a random symbol of two characters: english and russian, inserts into the text – Behavior Jul 10 '17 at 12:38
  • @Behavior That still does not explain why you use a list for each character (maybe google dictionary...) and in that case I can't tell whats wrong with the output. And why is 1 of the 2 (english/russian) always 2 characters..? – EpicKip Jul 10 '17 at 13:00

2 Answers2

1

Even if you replace letter with res.Append(x[1][rnd.Next(0, x[1].Length)]); later you still append base letter: res.Append(letter.ToString()); Try adding a condition:

        bool replaced = false;
        foreach (var x in alphavite)
        {
            if (x.Exists(e => e.EndsWith(letter.ToString())))
            {
                res.Append(x[1][rnd.Next(0, x[1].Length)]);
                replaced = true;
            }
        }
        if(!replaced)
            res.Append(letter.ToString());
Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
0

Maybe you should replace the English letter with its similar-looking Cyrillic equivalent from the Unicode character set, such as:

    var а = new List<string> { 'а', '\u0430' };
    var а = new List<string> { 'p', '\u0440' };
    var а = new List<string> { 'y', '\u0443' };
TomServo
  • 7,248
  • 5
  • 30
  • 47