Question 2

Type: Method and Control Structures

Blog This FRQ is about how to compare and perform operations with strings. I had to iterate through a guessed word and generate hints based on matching characters and presence in the hidden word, which is (un)surprisingly harder. Using methods and control structures and Java string operations, I was able to complete the hidden word challenge.

Question 2

public class HiddenWord {
    private String hidden;

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

    public String getHint(String guess) {
        String r = "";

        for (int i = 0; i < guess.length(); i++) {
            if (guess.charAt(i) == hidden.charAt(i))
                r += "" + guess.charAt(i);
            else if (hidden.indexOf(guess.charAt(i)) > -1)
                r += "+";
            else
                r += "*";
        }

        return r;
    }
    // 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