create a password with at least one lowercase letter in csharp

You can create a password with at least one lowercase letter in C# by generating a random password and checking if it contains at least one lowercase letter. Here's an example code:

main.cs
using System;
using System.Linq;

class Program
{
    static void Main(string[] args)
    {
        string password = GeneratePassword(10);
        while (!ContainsLowercase(password))
        {
            password = GeneratePassword(10);
        }
        Console.WriteLine(password);
    }

    static string GeneratePassword(int length)
    {
        const string validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!@#$%^&*()";
        var random = new Random();
        var chars = Enumerable.Range(0, length)
                              .Select(x => validChars[random.Next(0, validChars.Length)]);
        return new string(chars.ToArray());
    }

    static bool ContainsLowercase(string str)
    {
        return str.Any(c => char.IsLower(c));
    }
}
788 chars
30 lines

In this code, GeneratePassword method is used to generate a random password with the specified length. ContainsLowercase method checks if the password contains at least one lowercase letter.

The Main method generates a password with the specified length and keeps generating new passwords until it finds one that contains at least one lowercase letter. Once a valid password is found, it is printed to the console.

gistlibby LogSnag