java - How to write a recursive method to promote a String to all possible combinations, With no loops or arrays? -


public password(int length)//a constructor creates random password in given length public boolean ispassword(string st)//retruns true if string equals password. 

i need make recursive method... crack password... contains lower-case letters. can use: ispassword(),(charat, equals, length, substring) no loops, no arrays...

this ive done far, got "a" string right length:

/**  * recursive method, cracks , return   * right string of password p.  * @param p password object crack.  * @param length length of password.  * @return string combination of password.  */ public static string findpassword(password p,int length) {     string astring;//to store "aaa.." string in length of p     string password;//to store password      astring = findpassword("a",length);//recursion method, gets string of in length     password = findpassword(astring, p, 0);//recursion method, finds password      return password; } public static string findpassword(string startstring, int length) {   //recursion make "aaa..." string in length of n.     if (startstring.length() != length )//if not in length     {         startstring += "a";        //add "a" character         if(startstring.length() == length)//if in length,          {                     //return string             return startstring;         }     }                  //call recursion if still     return findpassword(startstring,length);//not in length } 

now need promote string possible combinations... , have no idea how without loops or array... thanks!

i have answer you.

the idea crack password run through possible combinations. combinations sequence 1,2,3,4, ... (iirc b-adic sequences term in math).

for decimal sequence, have digits 0-9 , rule overflow next number if reach end of alphabet (9 in case).

the decimal counting system can applied anything: binary numbers have alphabet of 2 (0,1). hex numbers have alphabet of 16 (0-9,a-f).

the passwort looking has alphabet of allowed character set in password. set represented in array of characters. array defines order , size of set. iterate passwords, apply counting algorithm password-"number".

the following code this:

public class passwordfinder {      private static final string password = "zxy";     private static final char[] alphabet = "abcdefghijklmnopqrstuvwxyz".tochararray();      boolean ispassword(string pass) {         return (pass.equals(password));     }      boolean findpassword(string password) {         while (!ispassword(password)) {             password = next(password);         }         return true;     }      /* recursive variant, require (alphabet.length^(password.length+1)) - 1 recursions */      boolean findpasswordrecursive(string password) {         if (ispassword(password)) {             return true;         }          return findpasswordrecursive(next(password));     }      private string next(string password) {         if( password.length() == 0 ) {             return "" + alphabet[0];         }          char nextchar = password.charat(password.length()-1);         int idx = (nextchar - alphabet[0] + 1) % alphabet.length;         if( idx == 0 ) {             return next(password.substring(0, password.length() - 1)) + alphabet[0];         }          return password.substring(0, password.length() - 1) + alphabet[idx];     } } 

the next function counting:

if receive no password (you can apply null check here well), create 1 first "digit" our alphabet:

        if( password.length() == 0 ) {             return "" + alphabet[0];         } 

we select char counted up. last digit of sequence, last char of string:

        char nextchar = password.charat(password.length()-1); 

to count, calculate index of char in alphabet (index magic, substract first char found char), index increased 1. handle "end of alphabet", use modulo operator. return 0 when reached end of alphabet.

        int idx = (nextchar - alphabet[0] + 1) % alphabet.length; 

when reach end of alphabet, need overflow next digit , add 1 that: "9" + 1 => "10". since overflowing recursive, use our counting function add 1 each digit in password , overflow accordingly. this, add 1 prefix of last digit , reset last digit "0": "89" + 1 => next("8") + "0"

        if( idx == 0 ) {             return next(password.substring(0, password.length() - 1)) + alphabet[0];         } 

if need not overflow, increase last digit.

        return password.substring(0, password.length() - 1) + alphabet[idx];     } 

Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -