Jump to content

Cryptogram Puzzle help

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
Renee55

Renee55

    Newbie

  • Members
  • Pip
  • 5 posts
Does anyone know how to write a simple code for cryptogram puzzles for a website? You enter a passage and then encrypt the passage to become numbers. You type "hello" and it would come out to be:
_ _ _ _ _
2 6 8 8 10

Thanks for any help.

#2
Momerath

Momerath

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 243 posts
Just something I threw together, to give you an idea. Most of the code deals with formatting :)

        static String Crypto(String source) {

            StringBuilder sb = new StringBuilder();

            char[] ca = source.ToUpper().ToCharArray();

            int[] code = Shuffled();


            foreach (char c in ca) {

                if (c < 'A' || c > 'Z') {

                    if (c == ' ') {

                        sb.Append(' ');

                    }

                    sb.Append(c);

                } else {

                    sb.Append(' ');

                    sb.Append(code[c-'A']).ToString();

                }

            }


            return sb.ToString().Trim();

        }


        

        static int[] Shuffled() {

            int j;

            int temp;

            int[] result = new int[26];

            Random r = new Random();


            for (int i = 0; i < result.Length; i++) {

                result[i] = i + 1;

            }


            for (int i = result.Length - 1; i > 0; i--) {

                j = r.Next(i);

                temp = result[i];

                result[i] = result[j];

                result[j] = temp;

            }


            return result;

        }