Generate all possible letter combinations of a given length in C#

The example Generate all possible three-letter words in C# uses three simple nested for loops to generate three-letter words. This example uses the following code to make a List holding words of any given length.

// Generate combinations of num_letters letters.
private List GenerateLetterCombinations(int num_letters)
{
    List values = new List();

    // Build one-letter combinations.
    for (char ch = 'a'; ch <= 'z'; ch++)
    {
        values.Add(ch.ToString());
    }

    // Add onto the combinations.
    for (int i = 1; i < num_letters; i++)
    {
        // Make combinations containing i + 1 letters.
        List new_values = new List();
        foreach (string str in values)
        {
            // Add all possible letters to this string.
            for (char ch = 'a'; ch <= 'z'; ch++)
            {
                new_values.Add(str + ch);
            }
        }

        // Replace the old values with the new ones.
        values = new_values;
    }

    return values;
}

The code first creates the 1-letter words a through z. Then for each additional letter that it must add, it loops through the existing words. For each existing word, the program loops through the letters a through z and creates a new word with that letter appended. For example, if the code is considering the existing word bad, it now creates the words bada, badb, badc, badd, etc.

The looping code adds the new words to a new List. When it has finished, it replaces the values List with the new List. After it finishes building all of the strings, it returns the List.

Note: Don't run this example for very long strings, at least until you know how your computer will perform. The program generates 26N N-letter words so the number of words grows very quickly. For example, 265 = 11,881,376 and 266 = 308,915,776.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments

  • 3/28/2012 8:51 AM Elias wrote:
    Hi I have a question. Is it also possible to use a script to generate a password. Let say to generate a combination of words from a word list with different length. Additionally all words will have to be joined but a number 5, i.e abs,bka,se,gamma=> abs5bka5se5gamma. Also as part of the password generation the script needs to capitalize a word . At the beginning of every new generated combination of passwords a quation mark should be set and. like this "Abs5bka5se5gamma or "abs5Bka5se5gamma etc. Many thanks for any take on this
    Reply to this
    1. 4/1/2012 5:31 PM Rod Stephens wrote:
      You should be able to do that. Just pick random letters for each word and concatenate them together with the 5s inserted. You can capitalize the first letters as you generate the string or after the fact.

      Note that any system that uses the Random class to generate random strings is not cryptographically secure. In theory, at least, someone who knew your method for generating passwords could list the passwords and use them to try to attack your system. If you really need something secure, look into the crypto API.
      Reply to this
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.