WebSolution: 1. public int countX (String str) {. 2. if (str.equals ("")) return 0; 3. if (str.charAt (0) == 'x') return 1 + countX (str.substring (1)); 4. else return countX (str.substring (1)); http://www.javaproblems.com/2012/11/coding-bat-java-string-2-counthi-given.html
Recursion - 1 (countX) Java Solution Codingbat.com - YouTube
Webcodingbat-Solutions / Recursion-1 / countHi2 / src / countHi2.java Go to file Go to file T; Go to line L; Copy path Copy permalink; This commit does not belong to any branch on this … WebcountHi public int countHi (String str) { int result = 0; for(int i = 0; i < str.length () - 1; i++) { if(str.substring (i, i+2).equals ("hi")) { result++; } } return result; } catDog public boolean catDog (String str) { int cat = 0; int dog = 0; for(int i = 0; i < str.length () - 2; i++) { if (str.substring (i, i+3).equals ("cat")) cat++; the scrub shoppe nashville tn
Return the number of times that the string "hi" appears anywhere …
WebIf you are counting occurrences in recursion, an easy formula is to create a base case to terminate on, then provide an incremental return, and finally a return that will aid in reaching the base case without incrementing. WebGiven a string, compute recursively (no loops) a new string where all the lowercase 'x' chars have been changed to 'y' chars. changeXY ("codex") → "codey" changeXY ("xxhixx") → "yyhiyy" changeXY ("xhixhix") → "yhiyhiy" Solution: 1 public String changeXY (String str) { 2 if (str.equals ("")) return str; 3 my phones keyboard won\u0027t show up