id/email
password
forgot password | create account
about | help | prefs
ReadingBatcode reading practice

 

 

For LoopsForLoop3

prev  |  next  |  chance

The .substring() method returns a portion of a string.

public static int loopIt(String str, int x) {
    int result = 0;

    for (int i = x; i < str.length(); i++) {
        if (str.substring(i, i + 1).equals("a"))
            result++;
    }

    return result;
}
Function Call  Return Value
loopIt("athenian", 1)
loopIt("apples", 2)
loopIt("hello", 3)
loopIt("alphabet", 0)
loopIt("aaaaa", 3)
loopIt("aaaaa", 4)
loopIt("aaaaa", 5)

Experiment with this code on Gitpod.io

⬅ Back