C# .Net code for All possible Combination of words using English Alphabets with user specific word length.

Bangalore: Here we are going to discuss about some programmatic implementation of permutation and combination. We all heard this kind of questions in our school times.
How many combination of   9 or 6(whatever ) letter words can make by using  English alphabets,so at that time we where used combination. 26C9 etc .
And we got some big values, Here we are going to check what all the combination of words are creating, actually hackers uses this kind of method when they know the password length, first they will create all possible passwords and send it to server using some scripts Its a kind of trial and error method.

So here we are going to tell this code in C# .net.
We will create a function there we will pass word length as a parameter and it will generate all possible words.

so here we go.

So this is our method. Here we will pass length of the word.

public static IEnumeable<string> getwrodLength(Int32 length)
{

     if (length <= 0)
            {
                yield break;
            }


  for(char c='a';c<='z';c++)
    {

    if (length > 1)
            {


               foreach(string words in getwrodLength(length-1)
                {
                   yield return c+words ;
                 }
              }
        else
               {
                yield return "" + c;
                }
           
     }
}

Calling your method from page_load,button click or any where

foreach(var val in getwrodLength(9))
{
    Console.WriteLine(val);
}

So now the values it will return in  Console, But IF you want to see the results in .csv or text file
You can use the following code.

foreach(var val in getwrodLength(9))
{

  StreamWriter sw=new StreamWriter(@"C:\Tony\words.csv",true);

              sw.WriteLine(String.Format("{0}",val));
              sw.Flush();
              sw.Close();
              sw.Dispose();

}

Thats It.
Check for Entertainment News.
www.shinestars.tk
Check for Job related queries.
http://click4jobz.blogspot.in/

0 comments: