1. i have a little coding project and i need a little help:

    i have the given class Password
    constructor: public Password(int i)
    which constructs a random password
    with the lower-case letters a-z
    in the given length

    it also has 2 methods:
    pubic String getPassword() which returns the password
    public boolean isPassword(string st) which checks if the password is correct


    i need to make the method
    public static string findPassword(Password p, int length)
    which will try all possible password combinations until it will find the password

    -I cant use loops in this project, only recursion
    -I cant use getPassword()

    please help
    thanks ;)
     
    #1
  2. (one loop would definately help here already, but without loops this may be a solution):

    You can have a function f, which will recursively add 1 character to the current password.

    Code:
    public void function(String pass){
    
    if(isPassword(pass)){
       System.out.println(pass);
      return;
    }
    
    function(pass + "a");
    function(pass + "b");
    function(pass + "c");
    .....
    function(pass + "z");
    
    }
    This will always terminate, assuming that it indeed is a password of length i with only a-z characters.

    However, this will increase your stack within no-time, so you may actually run out of memory (or just have to wait a very long time for larger passwords) before it finds it.


    Hope this helped ^.^

    EDIT:
    with one for-loop this would still do the same, but be a lot quicker to write down for you tough:
    Code:
    public void function(String pass){
    
    if(isPassword(pass)){
       System.out.println(pass);
      return;
    }
    
    for(int i = 97; i < 123; i++)
    {
    //this will go through a-z as well, as the a has an ascii value of 97, and z has one of 122. Everything in between i= b-y.
      function(pass + Character.getName(i));
    }
    
    }
     
    #2
    • Like Like x 1
    • Useful Useful x 1
  3. thank you sooo much ;)
    i think this is just what i was looking for
    :)
     
    #3

Share This Page