user2738052
user2738052

Reputation: 23

Replacing characters in windows form application

I need to make a windows form application in c# that provides the user with a textbox, and upon clicking a button it changes the letters in the first textbox to a replacement in the other one.

For example:
if I type "apple" in the first textbox and I have replaced "a" with "b" and "p" with "o" it should spell "baoole".

This process also has to work in reverse. I don't know how to accomplish this.

I tried using .Replace with every pair of letters in the alphabet ex: "a","b"; "c","d";, But it only replaced the first letters so if I typed "c" it would not change to "d". Once I tried to then replace "d","c"; it overlapped and my program wouldn't work. I then tried this:

if (richTextBox1.Text.Contains("a"))
{
    richTextBox2.Text=richTextBox1.Text.Replace("a", "b");
}
if (richTextBox1.Text.Contains("b"))
{
    richTextBox2.Text=richTextBox1.Text.Replace("b", "a");
}

But it only successfully replaced the first character. I am sorry if I missed anything obvious, I am learning c# and am eager to learn more. Thank you for your time and knowledge.

Upvotes: 0

Views: 3185

Answers (5)

Taha
Taha

Reputation: 2316

        string x = richTextBox1.Text;
        string result = "";
        for (int i = 0; i < x.Length; i++)
        {
            char c = x[i];
            if (c % 2 == 0)
            {
                c--;
            }
            else
            {
                c++;
            }
            result += c;
        }
        richTextBox2.Text=result;

If the text is "bad" this code will replace it with "abc" and will do the same in the reverse case, I think this is what you are looking for .

Upvotes: 2

Vyacheslav Yudanov
Vyacheslav Yudanov

Reputation: 451

Try it:

var replacements = new Dictionary<char, string>();
replacements.Add('a', "b");
replacements.Add('b', "a");
var inputString = "abc";
var etalonString = "bac";
var resSB= new StringBuilder();
foreach(var letter in inputString)
{
    if(replacements.ContainsKey(letter))
        resSB.Append(replacements[letter]);
    else
        resSB.Append(letter);
}
var resString = resSB.ToString();

Upvotes: 0

Simon Whitehead
Simon Whitehead

Reputation: 65077

It appears you're just trying to make each character be the next character in the alphabet. If that is the case.. I would use a StringBuilder and iterate over each character. Something like this:

private string Encrypt(string text)
{
    var content = new StringBuilder(text);

    for (int i = 0; i < content.Length; i++) {
        if (content[i] == 'z') {
            content[i] = 'a';
            continue;
        }
        if (content[i] == 'Z') {
            content[i] = 'A';
            continue;
        }

        content[i]++;
    }

    return content.ToString();
}

Basically, you can iterate over every character in the string and add 1 to it. If you encounter a Z or z.. then just round it out to A or a respectively and move on.

"Decryption" is the reverse of that:

private string Decrypt(string text) {
    var content = new StringBuilder(text);

    for (int i = 0; i < content.Length; i++) {
        if (content[i] == 'a') {
            content[i] = 'z';
            continue;
        }
        if (content[i] == 'A') {
            content[i] = 'Z';
            continue;
        }

        content[i]--;
    }

    return content.ToString();
}

That is, if you encounter an A or a, change it to Z or z respectively. Using the above, you can call it like this:

richTextBox2.Text = Encrypt(richTextBox1.Text); // Encrypt it
richTextBox2.Text = Decrypt(richTextBox2.Text); // Decrypt it

Click here to see a live sample of it running

Upvotes: 1

k&#243;dfodr&#225;sz
k&#243;dfodr&#225;sz

Reputation: 121

If I understand well what you would like to do:

You have "abba", and would like to do the following replacements { 'a' -> 'b', 'b' -> 'c' }. If you would use consecutive replaces this would give "cccc", but you would like to get "bccb".

If this is the case, i do not know any out of the box solition in the .net library, but you should write your own class to do this. I'd do it the following way:

  • The class gets a string (in the constructur), and splits it to a character array.
  • The class has a method to add transliteration pairs to its lisf of these steps.
  • The class has a method to do the transliteration.
    1. This method creates a bitmap, to track which character has been replaced.
    2. It scans the array and saves each transliteraated chracter to a target array.
    3. It flags that position as 'done'.
    4. When doing transliterations it skips the 'done' fields.
    5. finally it fills the non-transliterated fields, and returns a string constructed from the result character array.

This architecture lets you make the translitation easily reversible. As you stated you are learning C# (and I guess programming as well), thus I only wrote this guide to a possible implementation.

Upvotes: 0

Karthik AMR
Karthik AMR

Reputation: 1714

Every string is a character array. So you need to make a loop over that character array (ie., string) and replace each and every character.

    string richTextBoxString = richTextBox1.Text;
    foreach(char ch in richTextBoxString )
    {
    if(ch=='a')
        Convert.ToString(ch).Replace("a", "b");

    //likewise for all characters you need to code
    }
    richTextBox1.Text=richTextBoxString ;

Any clarification please ask.

Upvotes: 1

Related Questions