1 Replies - 348 Views - Last Post: 23 January 2020 - 05:27 AM Rate Topic: -----

#1 Never Saint   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 25-August 19

Problem with little program (StringBuilder)

Posted 23 January 2020 - 05:12 AM

I've been making this program for practice. It's a little coding program which takes words and changes the symbols into other symbols and therefore codes it.

But I seem to have a strange problem. I take the word "abba" and it codes it in "122" which is ok, but when I put "1221" it doesn't decode it into "abba" but "uvvu". Why do you think that is? It seems to only have problems when it comes to "decoding" number characters. Also when I write a very short key section to only code letters a & b into 1 & 2 the process works, but when I put key section for all the letters and some symbols it starts having problems. So what do you think is the problem?

Here's the code

StringBuilder word = new StringBuilder("1221");
            Console.Write("Do you want to code or decode (c or d)? ");
            string kid = Console.ReadLine();

            if (kid == "c")
            {
                word.Replace("a", "1");
                word.Replace("b", "2");
                word.Replace("c", "3");
                word.Replace("d", "4");
                word.Replace("e", "5");
                word.Replace("f", "6");
                word.Replace("g", "7");
                word.Replace("h", "8");
                word.Replace("i", "9");
                word.Replace("j", "a");
                word.Replace("k", "b");
                word.Replace("l", "c");
                word.Replace("m", "d");
                word.Replace("o", "e");
                word.Replace("p", "f");
                word.Replace("r", "g");
                word.Replace("s", "h");
                word.Replace("t", "i");
                word.Replace("u", "j");
                word.Replace("v", "k");
                word.Replace("w", "r");
                word.Replace("x", "l");
                word.Replace("y", "m");
                word.Replace("z", "n");
                word.Replace("n", "z");
                word.Replace("'", "x");
                word.Replace(",", "y");
            }
            else if (kid == "d")
            {
                word.Replace("1", "a");
                word.Replace("2", "b");
                word.Replace("3", "c");
                word.Replace("4", "d");
                word.Replace("5", "e");
                word.Replace("6", "f");
                word.Replace("7", "g");
                word.Replace("8", "h");
                word.Replace("9", "i");
                word.Replace("a", "j");
                word.Replace("b", "k");
                word.Replace("c", "l");
                word.Replace("d", "m");
                word.Replace("e", "o");
                word.Replace("f", "p");
                word.Replace("g", "r");
                word.Replace("h", "s");
                word.Replace("i", "t");
                word.Replace("j", "u");
                word.Replace("k", "v");
                word.Replace("r", "w");
                word.Replace("l", "x");
                word.Replace("m", "y");
                word.Replace("n", "z");
                word.Replace("z", "n");
                word.Replace("x", "'");
                word.Replace("y", ",");
            }
            else
            {
                Console.WriteLine("Error. Answer must be c or d.");
                Console.ReadKey();
            }

            Console.WriteLine(word);
            Console.ReadLine();



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with little program (StringBuilder)

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6833
  • View blog
  • Posts: 28,345
  • Joined: 12-December 12

Re: Problem with little program (StringBuilder)

Posted 23 January 2020 - 05:27 AM

You are replacing 1 with a and 2 with b, but subsequently you replace a with j ... and then j with u.

Once you have understood the problem I suppose there are a few approaches that could solve this. One would be to iterate the characters of the word making replacements, so that you won't be "backtracking".
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1