Question

Use Java to answer the question (Occurrences of a specified character) Write a method that finds...

Use Java to answer the question

(Occurrences of a specified character)

Write a method that finds the number of occurrences of a special character in a string using the following header:

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 followed by a character then displays the number of occurrences of the character in the string.

In addition to the requirements described in the exercised, your solution must meet the following requirements:

  • Your method must match the method header (and signature) exactly (including parameter names)
  • Your method must not have any input or output --
    e.g. you may not read the input from the keyboard or output to the screen from within your method.
  • All input and output must occur in the main method of your program and pass the values to your method and use the return value to get the result for output. (See below)
  • The variable names you use for input and output must be different than the parameter names for your method (e.g. str and a)
  • Case matters, so your method should distinguish between counting 'b' vs. 'B'.
  • You must have a validation loop for each of the input string and the character. Empty strings are the only invalid inputs
0 0
Add a comment Improve this question Transcribed image text
Answer #1

String_counting.java

import java.util.*;
class String_counting{
   public static int count (String str, char a){
      
       int c=0;
       int len = str.length();
       for(int i=0;i<len;i++){
           if(str.charAt(i)==a){
               c++;
           }
       }
       return c;
   }
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       String stri;
       System.out.println("Enter a string:");
       stri = scan.nextLine();
       //Below while loop for checking the entered string is empty or not if the string is empty than it will run upto user enter a string
       while(stri.equals(null) || stri.equals("")){
           System.out.println("Invalid input,Please Enter correct string:");
           stri = scan.nextLine();
       }
      
       char c;
       System.out.println("Enter a character:");
       c = scan.next().charAt(0);
       while(c==' ')
       {
           System.out.println("Enter a character:");
           c = scan.next().charAt(0);
       }
       System.out.println("Given Character count in the given string is: "+count(stri,c));
       //count(stri,c);
   }
}

Add a comment
Know the answer?
Add Answer to:
Use Java to answer the question (Occurrences of a specified character) Write a method that finds...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified...

    Please write code in C++ and include all headers not bits/stdc: 17.10 (Occurrences of a specified character in a string) Write a recursive function that finds the number of occurrences of a specified letter in a string using the following function header. int count(const string& s, 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...

  • 8.4 in python function that checks whether a string is a valid password. Suppose the pas...

    8.4 in python function that checks whether a string is a valid password. Suppose the pas rules are as follows: . A password must have at least eight characters - A password must consist of only letters and digits. ■ A password must contain at least two digits. Write a program that prompts the user to enter a password and displays valid password if the rules are followed or invalid password otherwise (Occurrences of a specified character) Write a function...

  • Java Counts the occurrences of each digit in a string

    Write a method that counts the occurrences of each digit in a string using the following header: public static int[] count(String s) The method counts how many times a digit appears in the string. The return value is an array of ten elements, each of which holds the count for a digit. For example, after executing int[] counts = count("12203AB3"), counts[0] is 1, counts[1] is 1, counts[2] is 2, and counts[3] is 2. Write a test program that prompts the...

  • java 2. Write a recursive method to return the number of occurrences a character c in...

    java 2. Write a recursive method to return the number of occurrences a character c in a string s. c and s should be parameters.

  • MUST BE IN JAVA AND USE RECURSION Write a recursive method that returns the number of...

    MUST BE IN JAVA AND USE RECURSION Write a recursive method that returns the number of all occurrences of a given word in all the files under a directory. Write a test program. Use the following header: public static long findInFile(File file, String word)

  • In this assignment, you will be creating three static methods as described below: The method duplicate...

    In this assignment, you will be creating three static methods as described below: The method duplicate should accept one parameter of type String (named “str”). The method should return a String with every character in str repeated n times, where n is the length of str if str has an odd number of characters e.g. duplicate("Hat") should return "HHHaaattt" and n is double the length of str if str has an even number of characters E.g. duplicate("Hi") should return "HHHHiiii"....

  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • i am trying to write a subroutine in assembly that removes all occurrences of a given...

    i am trying to write a subroutine in assembly that removes all occurrences of a given character in a string. The subroutine takes two parameters: the string pointer, and the character to be removed. Write a C code that calls this subroutine. The string is defined in the C source code file as global. Assume the assembly and C code are in separate files. Use Keil/uVision to test your program where the C code should ask the assembly subroutine to...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • Step 2: Create the method specified below. static int GetValidWidth(string prompt) Input parameters prompt: message for...

    Step 2: Create the method specified below. static int GetValidWidth(string prompt) Input parameters prompt: message for user describing the input required Returns A non-zero width entered by the user Step 3: Create the method specified below. static int GetValidHeight(string prompt) Input parameters prompt: message for user describing the input required Returns A non-zero positive height entered by the user Step 4: Create the method specified below. static void DisplayBox(int width, int height, char fillChar) Input parameters width: width of each...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT