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]
3 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]3 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
Which term describes a secure, individual, private cloud-computing model hosted within a public cloud?
grin007 [14]

The term that describes a secure, individual, private cloud-computing model hosted within a public cloud is A hybrid cloud.

<h3>What is this cloud about?</h3>

A hybrid cloud is known to be a system or a model which is said to be a form of a private cloud that is linked with public cloud infrastructure, and it is one that tends to give room for an organization to act workloads in two environments.

Note that In this model, the public cloud is known to be one that is seen and it is one that becomes an extension of the private cloud to make a single, uniform cloud.

Hence, The term that describes a secure, individual, private cloud-computing model hosted within a public cloud is A hybrid cloud.

Learn more about hybrid cloud from

brainly.com/question/27838623

#SPJ1

7 0
1 year ago
Copyrighted software illegally downloaded and sold by organized groups without a license to do so is called:
trasher [3.6K]
Copyrighted software illegally downloaded and sold by organized groups without a license is called Warez
5 0
3 years ago
In this lab, you declare and initialize variables in a C++ program. The program, which is saved in a file named NewAge.cpp, calc
Ne4ueva [31]

Answer:

#include <iostream>

using namespace std;

int main() {

int currentYear = 2020;

int myCurrentAge = 23;

int myNewAge=myCurrentAge+(2050-currentYear);

cout << "My Current Age is " << myCurrentAge << endl;

cout << "I will be " << myNewAge << " in 2050." << endl;

}

Explanation:

  • Initialize the currentYear with 2020 and myCurrentAge with 23.
  • Add myCurrentAge with the the result of (2015 - currentYear) and assign this result to myNewAge variable.
  • Finally display my current age and after that display the new age in 2050.

Output:

My Current Age is 23

I will be 53 in 2050.

6 0
3 years ago
All of the following are common causes of electrical accidents and injuries, EXCEPT:
mote1985 [20]
<h3>✽ - - - - - - - - - - - - - - - ~<u>Hello There</u>!~ - - - - - - - - - - - - - - - ✽</h3>

➷ It would be D. I don't really see how the plug itself is a cause of electrical accidents.

<h3><u>✽</u></h3>

➶ Hope This Helps You!

➶ Good Luck (:

➶ Have A Great Day ^-^

↬ ʜᴀɴɴᴀʜ ♡

7 0
3 years ago
Code is the code or instructions used to create a Web page or<br> program.
Paha777 [63]
Answer:
HTML
Explanation:

5 0
2 years ago
Other questions:
  • i see tabs named mowed and Ramsey i tried shredding them and deleting them and ending all the processes but they start duplicati
    13·1 answer
  • Design a Geometry class with the following methods: A static method that accepts the radius of a circle and returns the area of
    15·1 answer
  • The inability of BAE Automated Systems to create an automated baggage handling system led to a significant delay in the opening
    10·1 answer
  • The hexadecimal number system uses alphabets A to F to represent values_ to _
    12·2 answers
  • What is the first computer ever made?????
    9·2 answers
  • The parameter passing mechanisn used in C is
    5·2 answers
  • YOU WANT TO DISCARD YOUR OLD COMPUTER AND WANT TO SECURELY ERASE THE DATA FROM YOUR HARD DRIVE. WHAT SOFTWARE CAN YOU USE AND WH
    9·2 answers
  • What do u think a creative app must have? <br><br> Please answer the question ASAP!!
    5·1 answer
  • A class is a type of object that defines the format of the object and the actions it can perform. True False
    14·1 answer
  • Determine the value of a and b at the end of the following code segment:
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!