1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Stolb23 [73]
4 years ago
12

Write a recursive method that finds the number of occurrences of a specified letter in a string using the following method heade

r: public static int count(String str, char a) For example, count("welcome",' e') returns 2. Write a test program that prompts the user to enter a string and a character, and displays the number of occurrences for the character in the string.
Computers and Technology
1 answer:
Dafna1 [17]4 years ago
3 0

Answer:

//Java program to find the number of occurrences of a specified character in a string.

// Import the required packages.

import java.io.*;

import java.util.*;

//class declaration

class CharacterCount

{

// Main method.

public static void main (string args[ ] )

{

  try

      {

     // Taking the input from the user.

         System. out.print ("Type a string to analyze: ");

     // Creating the scanner class object.

         scanner input = new scanner (system. in);

         String str = input .next();

         System.out.print("Type a character to check: ")

         String temp = input.next();

         char c = temp.charAt(0);

     // Calling the method to get the character count.

         int count = <em>count</em>(str,c);

     // Displaying the result.

         system.out.print In("There are " + count + " " + c + " 's. ");

     // catching the exception.

         catch(Exception e)

         System.out.print In("Exception has occured in the class . Program will exit. ");

      // Exiting the system.

         System.exit(0);

         }

}

// Method to calculate the character count.

public static int count(String str, char a)

  {

    // checking for null string input.

      if(str.equals(""))

      {

       return 0;

      }

      else

      {

      int k = 0;

    // Character matching check.

      if ( str.substring(0, 1) . equals (Character.<em>toString</em>(a)) )

      {

   // Incrementing the counter.

      k++;

      }

   // Recursive call

      k += <em>count</em>(str.substring(1) , a);

              return k;

       }

     }

}

Explanation:

Output should show: Type a string to analyze: welcome

                                   Type a character to check: e

                                   There are 2 e's.

You might be interested in
________ are hardware devices or software programs that direct messages as they travel between networks.
In-s [12.5K]
<span>Routers are hardware devices or software programs that direct messages as they travel between networks. the answer is routers</span>
5 0
3 years ago
Quality answers will be appriciated! :)​
Naddika [18.5K]
<h2>1.</h2>

  1. F
  2. T
  3. T
  4. T
  5. F
  6. F
  7. T

________

<h2>2.</h2>

  1. A
  2. B
  3. D5*E5
  4. A

6 0
3 years ago
Read 2 more answers
A stored program computer is designed to compute precisely one computation, such as a square root, or the trajectory of a missil
mr Goodwill [35]

Answer: False

Explanation:

 The given statement is false as, a stored program computer is one of the type of computer system which storing the program instruction in the form of electronic memory.

  • It perform different types of tasks in the sequence and it also enables the digital computer system more effective and flexible.
  • In this stored program computer the program instructions are get stored on the plugboards.

Therefore, the given statement is false.

4 0
4 years ago
Name all the keys of the home row.
Kaylis [27]

Answer:

Search key, a, s, d, f, g, h, j, k, l, : and ;, ¨ and ´, and the enter key.

5 0
3 years ago
Pradeep and his cousin went to the corner store to buy candy. His cousin paid and told Pradeep he could pay him back. "You owe m
NeTakaya

Answer:

"It is 80 cents"

Explanation:

In order to calculate how much this actually is, we would need to multiply this fraction by the value of a whole dollar which is 1. We can divide the fraction 4/5 and turn it into the decimal 0.80 which would make this much easier. Now we simply multiply...

0.80 * 1 = $0.80

Finally, we can see that 4/5 of a dollar would be 0.80 or 80 cents. Therefore Pradeep would answer "It is 80 cents"

8 0
3 years ago
Other questions:
  • Waterpower was first harvested by ancient societies using
    5·1 answer
  • In Java :
    11·1 answer
  • When you enter a two-digit year that is less than 30, Excel changes the year to _____. Question 7 options:
    7·1 answer
  • E gpa(grade point average) you need to have or you get kicked out of robotics for middle school?
    13·1 answer
  • A viral video is one that can infect your computer with malware. <br> a. True <br> b. False
    6·1 answer
  • Drugs of addiction act upon a portion of the Brain called the lambic system ? True or false.
    7·1 answer
  • To print a budget:________.
    9·1 answer
  • Anyone else having problems with edg?
    9·1 answer
  • Blockchain is often associated with Bitcoin and the financial services industry. However, it is applicable to almost every indus
    13·1 answer
  • Which is true for comments? Group of answer choices The compiler does not generate machine code for comments. The compiler conve
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!