Question 2

Type: Method and Control Structures

Blog This FRQ deals with operations and comparisons with strings. It is (un)surprisingly difficult to cycle through a guess word and provide suggestions based on matching letters and presence in the concealed word. I was able to finish the hidden word challenge by using Java string operations, control structures, and methods.

Question 2

public class HiddenWord {
    private String hidden;

    public HiddenWord(String h) {
        hidden = h;
    }

    public String getHint(String guess) {
        StringBuilder hint = new StringBuilder();

        for (int i = 0; i < guess.length(); i++) {
            char guessedChar = guess.charAt(i);
            char hiddenChar = hidden.charAt(i);

            if (guessedChar == hiddenChar) {
                hint.append(guessedChar);
            } else if (hidden.contains(String.valueOf(guessedChar))) {
                hint.append("+");
            } else {
                hint.append("*");
            }
        }

        return hint.toString();
    }

    // Testing method
    public static void main(String[] args) {
        HiddenWord hw = new HiddenWord("hello");
        System.out.println(hw.getHint("blalo")); // Example usage
    }
}

HiddenWord.main(null)
*+*lo