Generate words of a given length in C#

[generate words]

The example Generate three-letter words in C# uses three nested for loops to generate words that contain three letters. This example uses the following code to generate words of any given length.

// Generate words with num_letters letters.
private List GenerateLetterCombinations(int num_letters)
    List values = new List();

    // Build one-letter combinations.
    for (char ch = 'a'; ch <= 'z'; ch++)

    // 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, and so forth.

The looping code adds the new words to a new List. When it's finished the code replaces the old 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.

Download Example   Follow me on Twitter   RSS feed   Donate

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

3 Responses to Generate words of a given length in C#

  1. Elias says:

    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

  2. Rod Stephens says:

    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.

  3. Denise says:


    This is a message to the admin. Your website is missing out on at least 300 visitors per day. I have found a company which offers to dramatically increase your traffic to your website: They offer 1,000 free visitors during their free trial period and I managed to get over 30,000 visitors per month using their services, you could also get lot more targeted visitors than you have now. Hope this helps ­čÖé Take care.

Leave a Reply

Your email address will not be published. Required fields are marked *