/home/caleb/ASDV-Java/Semester 3/Assignments/lab5-recursion2_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/lab5/recursion2_calebfontenot/OccurencesOfSpecifiedCharacterInArray.java |
package edu.slcc.asdv.caleb.lab5.recursion2_calebfontenot;
import java.util.Scanner;
@author
public class OccurencesOfSpecifiedCharacterInArray {
public static void main(String[] args)
{
System.out.print("Enter a string: ");
Scanner input = new Scanner(System.in);
String s = input.nextLine();
char[] items = s.toCharArray();
System.out.print("Enter a character: ");
char ch = input.nextLine().trim().charAt(0);
System.out.println(ch + " appears " + count(items, ch) + " times.");
}
public static int count(char[] chars, char ch) {
return count(chars, chars.length - 1, ch);
}
public static int count(char[] chars, int high, char ch) {
if (high >= 0) {
return count(chars, high - 1, ch) + (ch == chars[high] ? 1 : 0);
} else {
return 0;
}
}
}