C# program to generate random strings

Here, we are going to learn how to generate random strings in C#?
Submitted by Nidhi, on October 13, 2020

Here, we generate a random string using the Path.GetRandomFileName() method and print the generated strings on the console screen.


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:");


Random generated strings:
Press any key to continue . . .


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.

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.