Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

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

No comments:

Post a comment

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