Generate three-letter words in C#

[generate three-letter words]

This example uses the following very simple code to generate three-letter words using the letters a through z.

// Make the items.
private void Form1_Load(object sender, EventArgs e)
    List<string> values = new List<string>();
    for (char ch1 = 'a'; ch1 <= 'z'; ch1++)
        for (char ch2 = 'a'; ch2 <= 'z'; ch2++)
            for (char ch3 = 'a'; ch3 <= 'z'; ch3++)
                values.Add(ch1.ToString() +
                    ch2.ToString() + ch3.ToString());

    lstCombinations.DataSource = values;
    lblCombinations.Text = values.Count + " combinations";

The code uses three nested loops to generate the letters. The outermost loop generates the first letter, the next loop generates the second letter, and the innermost loop generates the last letter. The result includes every possible combination of three letters in alphabetical order.

This example isn’t very complicated. My next entry will show how to generalize this method to create words of any length. You might try to figure out how to do it yourself before you read that entry.

Download Example   Follow me on Twitter   RSS feed   Donate

This entry was posted in algorithms, mathematics and tagged , , , , , , , , , , . Bookmark the permalink.