Skip to content

Instantly share code, notes, and snippets.

@gitaficionado
Last active April 3, 2017 12:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitaficionado/21e676354e289913cca6567bda2c876a to your computer and use it in GitHub Desktop.
Save gitaficionado/21e676354e289913cca6567bda2c876a to your computer and use it in GitHub Desktop.
(Occurrences of a specified character) Write a method that finds the number of occurrences of a specified character in a string using the following header: public static int count(String str, char a)
import java.util.Scanner;
public class DisplayChar {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a string: ");
______________ = input.nextLine();
System.out.print("Enter a character: ");
____________ = input.nextLine().charAt(0);
int times = count(__ , ___);
System.out.println(ch + " _______________ " + times + (________ > 1 ? " times " : " time ") + "in " + __);
}
public static int count(String str, char a) {
int result = 0;
if (_________________() > 0)
result = _______________(str.substring(1), a) +
((str.charAt(0) == __) ? 1 : 0);
return ________;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment