Q:

C# program to generate random strings

belongs to collection: C# Basic Programs | String programs

0

C# program to generate random strings

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program:

The source code to generate random strings is given below. The given program is compiled and executed successfully on Microsoft Visual Studio.

///C# program to generate random strings.

using System;
using System.IO;

class Program
{
    public static string GenerateString()
    {
        string str = Path.GetRandomFileName();
        str = str.Replace(".", "!"); 
        return str;
    }
    static void Main()
    {
        int i = 0;
        Console.WriteLine("Random generated strings:");
        for(i=0;i<5;i++)
            Console.WriteLine("\t"+GenerateString());
        
    }
}

Output:

Random generated strings:
        znll05u3!rei
        wohzt02z!dux
        xfgymoni!dod
        b2wyq1kq!1lg
        q34d4cch!mux
Press any key to continue . . .

Explanation:

Here, we created a class Demo that contains two static methods GenerateString() and Main().

In the GenerateString(), we get random generated string using Path.GetRandomFileName() and then replace the '.' by exclamation sign '!' and return to the Main() method.

In the Main() method, we declared an integer variable 'i' and run a loop and get randomly generated strings and print them on the console screen.

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | String programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C# program to encrypt and decrypt a string using R... >>
<< C# program to count the total number of vowels in ...