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