Question

Write a method called countChar which accepts a string parameter and a character parameter and returns...

Write a method called countChar which accepts a string parameter and a character parameter and returns an integer, that is:

int countChar (String s, char c)

This method should count the number of occurrences of the character c within the string s, and return the count to the caller.

Also write a main method that tests countChar. All of the print statements and user interaction belong in the main method, not in countChar.

Homework Answers

Answer #1
import java.util.Scanner;

public class CountCharacters {

    public static int countChar(String s, char c) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c)
                ++count;
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a line of string: ");
        String s = in.nextLine();
        System.out.print("Enter a character: ");
        char ch = in.next().charAt(0);
        System.out.println(countChar(s, ch));
    }
}
Know the answer?
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
Similar Questions
C++ only: Please implement a function that accepts a string parameter as well as two character...
C++ only: Please implement a function that accepts a string parameter as well as two character arguments and a boolean parameter. Your function should return the number of times it finds the character arguments inside the string parameter. When both of the passed character arguments are found in the string parameter, set the boolean argument to true. Otherwise, set that boolean argument to false. Return -1 if the string argument is the empty string. The declaration for this function will...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as...
Write a recursive method repeatNTimes(String s, int n) that accepts a String and an integer as two parameters and returns a string that is concatenated together n times. (For example, repeatNTimes ("hello", 3) returns "hellohellohello") Write a driver program that calls this method from the Main program. Program should ask for input and be stored in the string and should return n amount of times.
You may notice that the function strchr in the <cstring> library searches for a character within...
You may notice that the function strchr in the <cstring> library searches for a character within a string, and returns the memory address (pointer) to where that character first occurs. 1.) Write your own version of the strchr function, int findchr( char text[ ], char target) , which returns the index of the first occurrance of the character target if it occurs within the string text. If target does not occur within the string, a -1 is to be returned....
Write a Java program with a method public String replaceChar(String p, int k, char c) {...
Write a Java program with a method public String replaceChar(String p, int k, char c) { } that given a String p, an int k, and a char c, returns a String with the kth character replaced by c. Of course, 0<=k<=p.length()-1, otherwise raise an exception or error.
JAVA: Write a method with the following header to return a string format to represent the...
JAVA: Write a method with the following header to return a string format to represent the reverse order of the integer: public static String reverse(int number) For example, reverse(3456) returns 6543 and reverse(809340) returns 043908. Write a test program that prompts the user to enter an integer then displays its reversal. Convert integer to string and print reverse of integer as string. Do not use built-in toString. Use loop.
Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1,...
Q5. Code a function, named CountNumerics, that accepts a parameter of a string , named str1, and returns the number of numeric characters (‘0’ .. ‘9’) + in the string. Helpful clues:   The string class has a function called length() that can be accessed by str1.length().   To access an individual character in the string; use string indexing (str1[i]) where I is an integer. Indexing of a string array starts at 0.
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens...
Write C++ code to: Create a function called “ReadFile” that: Accepts a filename as input Opens the file for reading Reads an integer from the file and returns it. Create a main function that Asks the user to input a file Passes that file to the “ReadFile” function Prints the returned integer to the screen Extra Credit: Set a default argument of “intfile.txt” for the “ReadFile” function Write an overloaded function of “ReadFile” that accepts two filenames and reads an...
Write a public method called containsDivPair that: • accepts three integer parameters • returns true if...
Write a public method called containsDivPair that: • accepts three integer parameters • returns true if any two of the numbers are divisible by 5, false otherwise
Write Java code that attempts to call a method named bootUp() which takes a String parameter...
Write Java code that attempts to call a method named bootUp() which takes a String parameter and returns an integer value. The String parameter should be read from a file named "bootConfig.txt" and is the only value in the file. If a BootUpException is thrown, print the Exception object's message. If a DriverException occurs, call the reboot() method and rethrow the original DriverException object. If any other type of Exception is thrown, print a generic error message to the console....
Write Java program Lab51.java which takes in a string from the user, converts it to an...
Write Java program Lab51.java which takes in a string from the user, converts it to an array of characters (char[] word) and calls the method: public static int countVowels(char[]) which returns the number of vowels in word. (You have to write countVowels(char[]) ).