We need to write the function to check the password entered is correct or based on the following conditions..

a) It must have atleast one lower case character and one digit.
b)It must not have any Upper case characters
c) length should be b/w 5-12.
d) It should not have any same immediate patterns like
abcanan1 : not acceptable coz of an an pattern
abc11se: not acceptable, coz of pattern 11
123sd123 : acceptable, as not immediate pattern
adfasdsdf : not acceptable, as no digits
Aasdfasd12: not acceptable, as have uppercase character

Solution :

package assignment1;

import java.util.Scanner;

/**
 * @author Snlkjha
 */
public class ValidPassword {
 
/*It must have atleast one lower case character*/  
public static boolean islower(String password){
    char pass[]=password.toCharArray();
    for(int i=0;i<pass.length;i++)
        if((int)pass[i]>=97 &&(int)pass[i]<=122) return true;
 
    return false;
}

/*It must not have any Upper case characters*/
public static boolean isupper(String password){  
    char pass[]=password.toCharArray();
    for(int i=0;i<pass.length;i++)
        if((int)pass[i]>=65 &&(int)pass[i]<=90) return false;
 
    return true;
}

/*It must have atleast one digit*/
public static boolean isdigit(String password){  
    char pass[]=password.toCharArray();
    for(int i=0;i<pass.length;i++)
        if(pass[i]-'0'>=0 && pass[i]-'0'<=9) return true;
 
    return false;
    }

/*length should be b/w 5-12*/
public static boolean length(String password){  
    char pass[]=password.toCharArray();
    int count=0;
    for(char c:pass) count++;
    return (count>=5 && count<=12);
    }

/*
 *It should not have any same immediate patterns like
 * abcanan1 : not acceptable coz of an an pattern
 * abc11se: not acceptable, coz of pattern 11
 * 123sd123 : acceptable, as not immediate pattern
 */
public static boolean pattern(String password){  
    char pass[]=password.toCharArray();
    int k=0;
    for(int i=1;i<pass.length;i++){
        if(pass[k]==pass[i]){
            int count=1;
            int t1=k;           /*Initial index of each char*/
            int t2=i;            /*Next index of same char*/
            while(++i<pass.length && pass[i]==pass[++k]) count++;
            if(t1+count==t2) return false;
            else{   k=t1+1;i=k; }          
        }else if(i==pass.length-1){ k++;i=k;    }  
    }
    return true;
    }

public static void isValidPassword(String password){
    String err_msg="";
    if(islower(password)==false) err_msg+="Password must have atleast one lower case characters.\n";
    if(isupper(password)==false) err_msg+="Password must not have any Upper case characters.\n";
    if(pattern(password)==false) err_msg+="Password should not have any same immediate patterns.\n";
    if(length(password)==false) err_msg+="Password length should be b/w 5-12.\n";  
    if(isdigit(password)==false) err_msg+="Password must have atleast one digit.\n";
    boolean result=islower(password) && isupper(password) && pattern(password) && length(password) && isdigit(password);
    if(result==true) System.out.println("Password is Valid.");
    else System.out.println(err_msg);
}

public static void main(String[] args){
    String password;
    Scanner sc=new Scanner(System.in);
    System.out.print("Enter Password :");
    password=sc.nextLine();
    isValidPassword(password);
    }

}

Popular posts from this blog

8 Bit Plane Slicing of an image in Image Processing

Code to upload multiple files simultaneously using JSP, Servlet .

STRING PALINDROME USING STACK AND QUEUE