Dinner Coding Time logo, a very stylized D

String-3 > countYZ

public int countYZ(String str) { int words = 0; for(int i = 0; i < str.length(); i++) { if( //put end-checker first so that it'll short-circuit if we're at the end and not try to get an undefined character for the space (i + 1 == str.length() || !Character.isLetter(str.charAt(i+1))) && (str.toLowerCase().charAt(i) == 'y' || str.toLowerCase().charAt(i) == 'z') ) { words++; } } return words; }