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
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.
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
Keep learning and sharing...
No comments:
Post a Comment