Developing Algorithms Using Strings

AP Computer Science A· difficulty 3/5

public static int countChar(String s, char c) {
    int n = 0;
    for (int i = 0; i < s.length(); i++) {
        if (/* missing */) n++;
    }
    return n;
}

Which condition replaces /* missing */?

  • A

    s.equals(c)

  • B

    s == c

  • C

    s.charAt(c) == i

  • D

    s.charAt(i) == c

    check_circle

Explanation

charAt(i) returns the i-th char; compare it to c with == since both are char primitives.

Want 10 more like this — adaptive to your weak spots?

Related questions