C:\> Rostislav Persion's Projects

.:: Inner Shuffle ::.
Shuffle the inner letters of words in a string




This program inputs a sentence and then shuffles the inner letters of every word. This was inspired by the internet meme where you can read a sentence even though the inner letters of every word are shuffled. As long as the first and last letters in each word are the same as the original letters.

SCRAMBLE A SENTENCE INSIDE THE BROWSER ...



C# SOURCE CODE ...


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
namespace Shuffle_002
{
    internal class Program
    {
        // a method for shuffling a string of characters
        static string ShuffleString(string inputstr)
        {

            char[] outputchr = inputstr.ToCharArray(0, inputstr.Length);

            Random rnd1 = new Random(Environment.TickCount);

            // use big O n^2 algorithm to randomly rearange letters in input string
            for (int x = 0; x < outputchr.Length; x++)
            {
                for (int y = 0; y < outputchr.Length; y++)
                {
                    int n1 = rnd1.Next(outputchr.Length);
                    int n2 = rnd1.Next(outputchr.Length);
                    char c1 = outputchr[n1];
                    char c2 = outputchr[n2];
                    outputchr[n2] = c1;
                    outputchr[n1] = c2;
                }
            }

            // convert char array into a string
            string outputstring = new string(outputchr);
            return outputstring;
        }

        // check to see if all letters are the same (NEW CODE)
        static bool AreAllLettersIdentical(string strIdent)
        {
            string firstletter = strIdent.Substring(0, 1);
            bool isident = true;
            // look for letters to not be the same as the first letter
            foreach (char ltr in strIdent)
            {
                if (ltr.ToString() != firstletter)
                {
                    isident = false;
                }
            }
            return isident;
        }

        // a method for turning a string into a string of scranbled words
        static string ShuffleSentence(string inputSentence)
        {
            // output buffer
            string outputBuffer = "";

            // split the input string into a string array of words
            string[] strChopped = inputSentence.Split(" ");

            // declare primary variables
            string firstLetter;
            string lastLetter;
            string middleString;
            string middleStringShuffled;

            foreach (string strCurrent in strChopped)
            {

                // if the string has more than one middle letters then rearange them
                if (strCurrent.Length > 3)
                {
                    firstLetter = strCurrent.Substring(0, 1);
                    lastLetter = strCurrent.Substring(strCurrent.Length - 1);
                    middleString = strCurrent.Substring(1, strCurrent.Length - 2);
                    middleStringShuffled = ShuffleString(middleString);

                    // check to see if all letters in a string are identical, if not keep shuffling (NEW CODE)
                    while ((!AreAllLettersIdentical(middleString)) & (middleString == middleStringShuffled))
                    {
                        middleStringShuffled = ShuffleString(middleString);
                    }


                }

                // if the string has less than two middle letters then just output the original string
                else
                {
                    firstLetter = "";
                    lastLetter = "";
                    middleStringShuffled = strCurrent;
                }

                // append output buffer
                outputBuffer += firstLetter + middleStringShuffled + lastLetter + " ";
            }

            return outputBuffer;

        }

        static void Main(string[] args)
        {

            // keep program going forever
            while (true)
            {

                // input sentence
                Console.ForegroundColor = ConsoleColor.Green;
                Console.Write("ENTER A SENTENCE: ");
                string strMain = Console.ReadLine();
                Console.WriteLine("");

                Console.ForegroundColor = ConsoleColor.Yellow;
                Console.WriteLine(ShuffleSentence(strMain));

                // create vertical spacing between lines
                Console.WriteLine("");
                Console.WriteLine("");

            }

        }
    }
}