[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Generate three-letter words in C#

[Generate three-letter words in C#]

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 the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.