Přidat otázku mezi oblíbenéZasílat nové odpovědi e-mailemVyřešeno C# Problém se záměnou znaků replace

Dobrý den,
mám další problém s replace, ale tentokrát jiný.
Když už mám doprogramováno, tak při stisku písmen od a až do m se písmena nezaměňují. Od n do z už ano. To samé od ů do ý a od 1 do 5.

Dá se to nějak řešit?

private void button1_Click(object sender, EventArgs e)
        {
            label2.Text = "Zašifrovaný text:";
            button2.Text = "Kopírovat zašifrovaný text do schránky";
            txt = textBox1.Text;
            result = "";

            result = txt.Replace('a', 'a');
            result = result.Replace('b', 'o');
            result = result.Replace('c', 'p');
            result = result.Replace('d', 'q');
            result = result.Replace('e', 'r');
            result = result.Replace('f', 's');
            result = result.Replace('g', 't');
            result = result.Replace('h', 'u');
            result = result.Replace('i', 'v');
            result = result.Replace('j', 'w');
            result = result.Replace('k', 'x');
            result = result.Replace('l', 'y');
            result = result.Replace('m', 'z');

            result = result.Replace('n', 'a');
            result = result.Replace('o', 'b');
            result = result.Replace('p', 'c');
            result = result.Replace('q', 'd');
            result = result.Replace('r', 'e');
            result = result.Replace('s', 'f');
            result = result.Replace('t', 'g');
            result = result.Replace('u', 'h');
            result = result.Replace('v', 'i');
            result = result.Replace('w', 'j');
            result = result.Replace('x', 'k');
            result = result.Replace('y', 'l');
            result = result.Replace('z', 'm');
            result = result.Replace('ů', 'ě');
            result = result.Replace('ě', 'ů');
            result = result.Replace('ž', 'ú');
            result = result.Replace('ú', 'ž');
            result = result.Replace('ý', 'š');
            result = result.Replace('š', 'ý');
            result = result.Replace('á', 'č');
            result = result.Replace('č', 'á');
            result = result.Replace('í', 'ř');
            result = result.Replace('ř', 'í');
            result = result.Replace('é', 'ď');
            result = result.Replace('ď', 'é');
            result = result.Replace('ň', 'ř');
            result = result.Replace('ř', 'ň');
            result = result.Replace('š', 'ť');
            result = result.Replace('ť', 'š');
            result = result.Replace('1', '6');
            result = result.Replace('2', '7');
            result = result.Replace('3', '8');
            result = result.Replace('4', '9');
            result = result.Replace('5', '0');
            result = result.Replace('6', '1');
            result = result.Replace('7', '2');
            result = result.Replace('8', '3');
            result = result.Replace('9', '4');
            result = result.Replace('0', '5');
Předmět Autor Datum
Protože to zaměníš za jiný znak a později ten znak změníš zase zpět. Jak už jsem ti psal minule, Rep…
Wikan 26.07.2015 18:22
Wikan
Mohl bys mi tedy prosím napsat, jak bys to udělal ty. Prosím napiš se stejnýma deklaracema jako můj… nový
Ryba 26.07.2015 18:26
Ryba
var fromChars = new [] {'a', 'b', 'c'}; var toChars = new [] {'n', 'o', 'p'}; var result = new char[… nový
Wikan 26.07.2015 18:35
Wikan
Díky za ochotu :) nový
Ryba 26.07.2015 20:00
Ryba
mimochodem, pokud by to melo byt jo rychle a pro nejakou ascii page, tak je lepsi si udelat pole 0..… nový
gilhad 27.07.2015 13:56
gilhad
Mám tam ještě nějakou chybu. Co je tam špatně? Nebo jak to mám opravit? [http://pc.poradna.net/file… nový
Ryba 27.07.2015 15:41
Ryba
Tohle už bude fungovat. var fromChars = new[] { 'a', 'b', 'c' }; var toChars = new[] { 'n', 'o', 'p… nový
Wikan 27.07.2015 15:48
Wikan
Do čeho to mám ještě konvertovat, abych to dal do textBoxu? Když dám textBox2.Text = result; tak… nový
Ryba 27.07.2015 15:57
Ryba
textBox2.Text = new string(result); nový
Wikan 27.07.2015 16:03
Wikan
Super. Díky za pomoc :) poslední
Ryba 27.07.2015 16:09
Ryba

Mohl bys mi tedy prosím napsat, jak bys to udělal ty. Prosím napiš se stejnýma deklaracema jako můj program. Stačí mi napsat třeba jen první tři. a-n, b-o, c-p. Díky :)

mimochodem, pokud by to melo byt jo rychle a pro nejakou ascii page, tak je lepsi si udelat pole 0..255, tam si to poskladat a konvertovat to primo z nej

PSEUDOKOD:

var pole char[255];
for (int i=0; i<256;i++) pole[i]=i;

i['a']='n';
i['b']='o';
.... //nebo to nacist z jineho pole, nebo lepe ze souboru

for (int i=0;i<txt.length;i++)txt[i]=pole[txt[i]];

// na x86 je na tohle dokonce instrukce v assembleru

Tohle už bude fungovat.

var fromChars = new[] { 'a', 'b', 'c' };
var toChars = new[] { 'n', 'o', 'p' };
var result = new char[txt.Length];
for (var i = 0; i < txt.Length; i++)
{
    var idx = Array.IndexOf(fromChars, txt[i]);
    result[i] = idx > -1 ? toChars[idx] : txt[i];
}
Clipboard.SetText(new string(result));

Zpět do poradny Odpovědět na původní otázku Nahoru