Question: So I had to write a test program that prompts the user to enter a list of characters in one line, and a character, and
So I had to write a test program that prompts the user to enter a list of characters in one line, and a character, and displays the number of occurences of the character in the list. I am supposed to provide a Flowchart and I feel like I am bad at making them and I need help designing a flowchart for the code provided.
import java.util.Scanner; public class main { public static int count(char[] chars, char ch) { int c = 0; for(int i = 0; i < chars.length; ++i) { if(chars[i] == ch) { c++; } } return c; }
public static int count(char[] chars, char ch, int high) { if(chars.length == high) { return 0; } else { return (ch == chars[high] ? 1 : 0) + count(chars, ch, high+1); } }
public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter a string: "); String str = in.nextLine(); System.out.print("Enter a character: "); char ch = in.next().charAt(0); System.out.println(ch + " appears " + count(str.toCharArray(), ch) + " times"); System.out.println(ch + " appears " + count(str.toCharArray(), ch, 0) + " times"); in.close(); }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
