Random String Generation

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

using System.IO;

 

namespace RandomStringGeneration

{

    class Program

    {

        static void Main(string[] args)

        {

            //Program to generate random strings of given length

            //from a given set of characters

            //Count and print the duplicate strings

            //Also print the max duplication count

            char[] characterSet = { ‘a’, ‘b’, ‘c’, ‘d’};

            int stringLength = 5;

            int numberOfStrings = 100;

            int duplicateCount = 0;

            int maxDuplication = 0, temp = 0;

            ArrayList stringCollection = new ArrayList();

            Random rand = new Random();

            StreamWriter sw = new StreamWriter(@"C:\Output.txt", false);

            for (int j = 0; j < numberOfStrings; j++)

            {

                string randomString = "";

                for (int i = 0; i < stringLength; i++)

                {

                    randomString = randomString + characterSet[rand.Next(characterSet.Length)];

                }

                stringCollection.Add(randomString);

                sw.WriteLine(randomString);

                Console.WriteLine("The random string generated is: " + randomString);

            }

            sw.Close();

            stringCollection.Sort();

            Console.WriteLine("Printing duplicate characters:");

            for (int i = 0; i < stringCollection.Count-1; i++)

            {

                if (stringCollection[i].Equals(stringCollection[i + 1]))

                {

                    if (temp == 0)

                    {

                        Console.WriteLine(stringCollection[i]);

                        Console.WriteLine(stringCollection[i + 1]);

                        duplicateCount = duplicateCount + 2;

                    }

                    else

                    {

                        Console.WriteLine(stringCollection[i + 1]);

                        duplicateCount++;

                    }

                    temp++;

                }

                else if (temp != 0 && !stringCollection[i].Equals(stringCollection[i + 1]))

                {

                    Console.WriteLine("====================");

                    if (temp > maxDuplication)

                    {

                        maxDuplication = temp;

                        temp = 0;

                    }

                    else

                    {

                        temp = 0;

                    }

                }

            }

            Console.WriteLine("Number of duplicate strings generated is: " + duplicateCount);

            if (duplicateCount != 0)

            {

                Console.WriteLine("Max duplication count is: " + (maxDuplication + 1));

            }

            else

            {

                Console.WriteLine("Max duplication count is: 0");

            }

        }

    }

}

 

The random string generated is: bacab
The random string generated is: ababc
The random string generated is: ccccc
The random string generated is: bccbc
The random string generated is: abacb
The random string generated is: acbba
The random string generated is: babab
The random string generated is: cacbb
The random string generated is: acaca
The random string generated is: aacac
The random string generated is: cabbc
The random string generated is: acaba
The random string generated is: bacba
The random string generated is: cacac
The random string generated is: bcbab
The random string generated is: abaca
The random string generated is: acbba
The random string generated is: bcaab
The random string generated is: acaac
The random string generated is: aabcb
Printing duplicate characters:
acbba
acbba
====================
Number of duplicate strings generated is: 2
Max duplication count is: 2
Press any key to continue . . .

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s