Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

How could I solve this logic using // int offset(const string& generated, const

ID: 3803720 • Letter: H

Question

How could I solve this logic using // int offset(const string& generated, const string& userInput); and // isdigit() and isalnum() utility functions?

In other words:

The computer will generate a string of 5 characters which I already have generated.

The total offset is computed by accumulating the absolute distance between two characters in the same position, one from the generated string and another from the input. For instance, the offset of "Game" and "Mag" is 81. The shorter string is padded with space(s). Therefore, |G - M| = 6, |a - a| = 0, |m - g| = 6, |e - (space)| = 69. In other words, the total offset is the sum of the ASCII code differences between the ith character in the generated string and the ith character of user input.

Half of that time it should be a NON Alphanumeric character.
In the prompt to the user this is shown as [%-!].
If the user enters ANY non-alphanumeric character it is considered a match. For this situation, you should add a "*" to your growing string.

The other half of time the wildcard character should be a single digit.
This is shown to the user as [0-9]
For this situation, you should add a "0" to your growing string.

As for alphabetic characters (not wild), half of the time it should be an UPPER case letter and
half of the time it should be a lower case letter

When the following is printed by the program: BIy[0-9][%-!] the user
should try to match the 5 character string having the
following specification:

The user is to try to type:

Typing error for positions with wild card characters is treated according to the following:
If [0-9] appears in the output, and the user does not type a digit, the offset for that position
should be the absolute difference between the character typed and ‘0’.

If [%-!] appears in the output, and the user does not type a non-alphanumeric character, the
offset for that position should be the absolute difference between the character typed and
‘*’.
For instance, the offset of Tw[0-9]v[%-!] and Twxv2 is 80. From the ASCII chart we have the
following decimal codes:
‘0’ has a code of 48
‘*’ has a code of 42
‘x’ has a code of 120
‘2’ has a code of 50
We get 80 because of the mismatch at 3rd and 5th character. The absolute difference for the 3rd
character is abs(120 – 48) == 72 and for the 5th character is abs(50 – 42) == 8. Therefore the
offset is 72+8 resulting in 80.

Thank you in advance

Explanation / Answer

public boolean containsBothNumbersAndLetters(String password) { boolean digitFound = false; boolean letterFound = false; for (char ch : password.toCharArray()) { if (Character.isDigit(ch)) { digitFound = true; } if (Character.isLetter(ch)) { letterFound = true; } if (digitFound && letterFound) { // as soon as we got both a digit and a letter return true return true; } } // if not true after passing through the entire string, return false return false; }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote