-2

I am working on an app integration with a website. Basically, I am a PHP developer but the android app is using Java and I am not very professional in it. I need to offer an option to change passwords via the app itself.

When the user enters a new password as a string, I want the app to make sure that no substrings of length 3 from the old password are used in the new password. This check should be case insensitive.

Eg. If a user has the old password "Angel173MN", he/she cannot opt for any new password that contains any of the substrings: nge,l17,73M etc

Can anyone please guide me how to do it?

cdpaiva
  • 93
  • 1
  • 7
Aga Rengma
  • 44
  • 7
  • 1
    Have you tried anything yet? Where exactly are you stuck? – Mureinik Dec 22 '18 at 11:04
  • Related : https://stackoverflow.com/questions/24435711/check-a-string-for-consecutive-repeated-characters – akash Dec 22 '18 at 11:07
  • Yes, I tried some ideas found on this site but its complex for me. I am actually unable to compare the two Passwords and finding out the common three chars in the same order.. – Aga Rengma Dec 22 '18 at 11:10
  • https://stackoverflow.com/questions/86780/how-to-check-if-a-string-contains-another-string-in-a-case-insensitive-manner-in – Aga Rengma Dec 22 '18 at 11:11
  • 1
    Not that closely related, @coder-croc. That question is about recognizing double letters such as `oo` or `pp` in a string. – Ole V.V. Dec 22 '18 at 11:14
  • ' String num1 = "Java"; String num2 = "Guava"; int count = 0; List charsChecked = new ArrayList<>(); for(int i = 0;i < num1.length();i++){ String charToCheck = num1.substring(i, i+1); if (!charsChecked.contains(charToCheck)) { count += StringUtils.countMatches(num2, charToCheck); charsChecked.add(charToCheck); } } System.out.println(count);' – Aga Rengma Dec 22 '18 at 11:15
  • @OleV.V. Yes, I agree. I thought it would be a good start for OP to find the solution. :) – akash Dec 22 '18 at 11:22

1 Answers1

3
    String oldPassword = "Angel173MN";
    String newPassword = "Tomato47nge";
    for (int i = 0; i < oldPassword.length() - 2; i++) {
        String sub = oldPassword.substring(i, i + 3);
        if (newPassword.contains(sub)) {
            System.out.println("Substring " + sub + " is part of both passwords");
        }
    }

Output:

Substring nge is part of both passwords

As @coder-croc points out in a comment, you can break the loop at the first match since you don’t need to find more matches. Since I understood the string operations as the heart of your question, I will leave the logic to yourself.

The String class in Java has pretty good support for many string operations.

Link: Documentation of String

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161