1
public class Utilities {

count()

Count non-overlapping instances of b in a.

1
2
java> count("ababababa", "aba")
java.lang.Integer res0 = 2

1
2
3
4
5
6
7
8
9
10
    public static int count(String a, String b) {
        int count = 0;
        int i = 0;
        int j = a.indexOf(b, i);
        while (j >= 0) {
            count++;
            j = a.indexOf(b, j + b.length());
        }
        return count;
    }

isVowel()

1
2
3
4
java> isVowel('a')
java.lang.Boolean res0 = true
java> isVowel('B')
java.lang.Boolean res1 = false

1
2
3
4
    public static boolean isVowel(char ch) {
        String chstr = String.valueOf(Character.toLowerCase(ch));
        return "aeiou".indexOf(chstr) != -1;
    }

countVowels()

1
2
java> vowelCount("foobar")
java.lang.Integer res2 = 3

1
2
3
4
5
6
7
    public static int vowelCount(String str) {
        int vowelsCount = 0;
        for (int i = 0, l = str.length(); i < l; i++) {
            if (isVowel(str.charAt(i))) vowelsCount++;
        }
        return vowelsCount;
    }

replaceVowels()

1
2
java> replaceVowels("foobar", 'x')
java.lang.String res3 = "fxxbxr"

1
2
3
4
5
6
7
8
9
    public static String replaceVowels(String phrase, char ch) {
        StringBuilder result = new StringBuilder(phrase);
        for (int i = 0, l = result.length(); i < l; i++) {
            if (isVowel(result.charAt(i))) {
                result.setCharAt(i, ch);
            }
        }
        return result.toString();
    }

1
} // Utilities