Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Wednesday, 24 May 2017

String based program


String program #14 (code to create string contain atleast 2 alphabet, 2 number, 2 special length, min 8 using regex)

Enter String: P4%_$23aTsU/
O/p: -
Perfect string matching all criteria

The program for the string is written in C# programming language and will accept a password as input. The logic is to check whether password contain 2 alphabet, 2 number, 2 special characters and minimum password of 8.
 
String based program

Note: If you are new to C# and Console Application. Try to code First C# Program

Note: Read articles on how to use Loops and Conditions.

Let’s find out a simple and easy way to code the program.

Practical Implementation:

using System;
using System.Text.RegularExpressions;

namespace patternProblem.String
{
    class String14 //check string contain atleast 2 alphabet, 2 number, 2 special length, min 8 using regex
    {
        public static void Main()
        {
            //string str = "Tes$f_use2d1";
            Console.Write("Enter String:");
            string str = Console.ReadLine();

            string strAlphabeRegex = "(?=(.*[a-zA-Z]){2})";
            string strNumberRegex = "(?=(.*[0-9]){2})";
            string strSpecialCharRegex = @"(?=(.*[`!@#$%\^&*\-_=\+'/\.,]){2})";
            string strMainPattern = @"(?=(.*[a-zA-Z]){2})(?=(.*[0-9]){2})(?=(.*[`!@#$%\^&*\-_=\+'/\.,]){2})(?=^.{8,15}$)";

            //Using Regex
            if (Regex.IsMatch(str, strAlphabeRegex))
            {
                if (Regex.IsMatch(str, strNumberRegex))
                {
                    if (Regex.IsMatch(str, strSpecialCharRegex))
                    {
                        if (Regex.IsMatch(str, strMainPattern))
                        {
                            Console.WriteLine("Perfect string matching all criteria");
                        }
                        else
                        {
                            Console.WriteLine("String length should be 8 to 15");
                        }
                    }
                    else
                    {
                        Console.WriteLine("String must contain atleast 2 special characters");
                    }
                }
                else
                {
                    Console.WriteLine("String must contain atleast 2 numbers");
                }
            }
            else
            {
                Console.WriteLine("String must contain atleast 2 characters");
            }
        }
    }
}

Output:
The input string1 here is password. So, based on the input, program will check whether string1 contain searched string. The output is shown below:

Enter String: P4%_$23aTsU/
Perfect string matching all criteria
Press any key to continue . . .


For any query, comment us below.

Skip to Main Table String based program

Previous - String based program #13


Click imagination hunt to read latest blogs.


Keep learning and sharing...

Featured post

Think that makes you rich and richer

 Napolean said: “You can think and grow rich, but if you can be brought up like most people with work and you won't starve, this wil...