Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that gets a String s and a character c as user input, and output

ID: 3730326 • Letter: W

Question

Write a program that gets a String s and a character c as user input, and outputs the number of occurrences of c in S.
NOTE: Your implementation must include the definition/implementation and use/calling of the recursive method charCount, which is passed a String and a character to its parameter variables, and returns the number of occurrences of the character in the String. —— Use the Design Recipe to design the algorithm for each method (main and others), showing all work as comments in the source code (.java) file. Using Eclipse, implement the algorithm as a program. Write a program that gets a String s and a character c as user input, and outputs the number of occurrences of c in S.
NOTE: Your implementation must include the definition/implementation and use/calling of the recursive method charCount, which is passed a String and a character to its parameter variables, and returns the number of occurrences of the character in the String. —— Use the Design Recipe to design the algorithm for each method (main and others), showing all work as comments in the source code (.java) file. Using Eclipse, implement the algorithm as a program. Write a program that gets a String s and a character c as user input, and outputs the number of occurrences of c in S.
NOTE: Your implementation must include the definition/implementation and use/calling of the recursive method charCount, which is passed a String and a character to its parameter variables, and returns the number of occurrences of the character in the String. —— Use the Design Recipe to design the algorithm for each method (main and others), showing all work as comments in the source code (.java) file. Using Eclipse, implement the algorithm as a program. Use the Design Recipe to design the algorithm for each method (main and others), showing all work as comments in the source code (.java) file. Using Eclipse, implement the algorithm as a program.

Explanation / Answer


import java.util.*;
import java.lang.*;
import java.io.*;

class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner reader = new Scanner(System.in);
String str = reader.nextLine();
char c = reader.next().trim().charAt(0);
int ans = charCount(str,c);
System.out.println(ans);
}

public static int charCount(String str,char c)
           {
               int length = str.length();
          
               if(str.charAt(0)==c)
               {
            if(length>1)
            {
            String temp =str.substring(1);
            return 1+charCount(temp,c);
            }
            else
            {
            return 1;
            }
               }
               else
               {
               if(length>1)
            {
                        String temp =str.substring(1);
                        return charCount(temp,c);
            }
            else
            {
            return 0;
            }
                }
             };
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote