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...

Tuesday, 23 May 2017

String based program

String program #13 (Check all characters in string 2 is contained in string1)

Enter string1: imagination
Enter string2: sarcastical
O/p: -
String2 doesnot contain all characters of string1

The program for the string is written in C# programming language and will accept two strings as input. The logic is to check all characters of string1 is contained in string2.
 
string based program by imagination hunt
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;

namespace patternProblem.String
{
    class String13          //check all characters in string 2 is contained in string1  
    {
        public static void Main()
        {
            Console.Write("Enter string1: ");
            string str1 = Console.ReadLine();
            Console.Write("Enter string2: ");
            string str2 = Console.ReadLine();

            int count = 0;
            StringBuilder strCharMatched = new StringBuilder();

            for (int i = str2.Length - 1; i >= 0; i--)
            {
                for (int j = 0; j <= str1.Length - 1; j++)
                {
                    if (str2[i] == str1[j])
                    {
                        strCharMatched.Append(str2[i]);
                        strCharMatched.Append(",");
                        count++;
                        break;
                    }
                }
            }

            if (str2.Length == count)
            {
                Console.WriteLine("String2 contain all characters in string1");
                Console.WriteLine("All characters that are matched: {0}", strCharMatched);
            }
            else
            {
                Console.WriteLine("String2 doesnot contain all characters in string1");
            }
        }
    }
}

Output:
Based on the input, program will check whether string2 contain all characters in string1. The output is shown below:

Enter string1: imagination
Enter string2: namingition
String2 contain all characters of string1

Enter string1: imagination
Enter string2: sarcastical
String2 doesnot contain all characters of string1

Enter string1: liveviar
Enter string2: rivale
String2 doesnot contain all characters of string1
Press any key to continue . . .


For any query, comment us below.

Skip to Main Table String based program

Previous - String based program #12

Next – String based program #14

Click imagination hunt to read latest blogs.


Keep learning and sharing...

Sunday, 21 May 2017

String based program

String program #12 (validate whether Password contain same 3(three) characters which exist in UserId)

Enter UserId: easytrip
Enter Password: hjabcfhj
O/p: -
Password accepted

The program for the string is written in C# programming language and will accept two strings as input. The logic is to check whether Password contain UserId. If yes, then the code must validate that password.
 
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;

namespace patternProblem.String
{
    class String12      //validate whether Password contain same 3(three) characters which exist in UserId
    {
        public static void Main()
        {
            Console.Write("Enter UserId: ");
            string txt_login_value = Console.ReadLine();
            Console.Write("Enter Password: ");
            string txt_pswd_value = Console.ReadLine();
           
            String12 obj = new String12();

            if (obj.UserIdContainSameThreeCharacterInPassword(txt_login_value, txt_pswd_value) == false)
            {
                Console.WriteLine("Invalid! Password contain same 3(three) characters which exist in UserId");
            }
            else {
                Console.WriteLine("Password accepted");
            }
        }

        public bool UserIdContainSameThreeCharacterInPassword(string txt_login_value, string txt_pswd_value)
        {
            for (int i = 0; i < txt_login_value.Length - 2; i++)
            {
                for (int j = 0; j < txt_pswd_value.Length - 2; j++)
                {
                    if (txt_login_value.Substring(i, 3) == txt_pswd_value.Substring(j, 3))
                    {
                        return false;
                    }
                }
            }

            return true;
        }
    }
}

Output:

So, based on the input UserId and password, program will check whether password contain same 3 characters that exist in UserId.

The output is shown below:

Case-1: Password is not same
Enter UserId: easytrip
Enter Password: hjabcfhj
Password accepted

Case-2 When password contain similar 3 characters that exist in UserId
Enter UserId: ghabcdkhabcdjabcf
Enter Password: hjabcfhj
Invalid! Password contain same 3 three characters which exist in UserId
Press any key to continue . . .


For any query, comment us below.

Skip to Main Table String based program

Previous - String based program #11

Next – 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...