Question

6.31 LAB: Count characters - methods ----- javascript please Write a program whose input is a...

6.31 LAB: Count characters - methods ----- javascript please

Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string.

Ex: If the input is:

n Monday

the output is:

1

Ex: If the input is:

z Today is Monday

the output is:

0

Ex: If the input is:

n It's a sunny day

the output is:

2

Case matters. n is different than N.

Ex: If the input is:

n Nobody

the output is:

0

Your program must define and call the following method that returns the number of times the input character appears in the input string.
public static int countCharacters(char userChar, String userString)

Note: This is a lab from a previous chapter that now requires the use of a method.

Homework Answers

Answer #1



import java.util.Scanner;

public class CountCharacters {

    public static int countCharacters(char userChar, String userString) {
        int count = 0;
        for(int i = 0; i < userString.length(); ++i) {
            if(userString.charAt(i)== userChar) {
                ++count;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        char ch = in.next().charAt(0);
        String str = in.nextLine();
        System.out.println(countCharacters(ch, str));
    }
}

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++) Write a program whose input is two characters and a string, and whose output indicates...
(C++) Write a program whose input is two characters and a string, and whose output indicates the number of times each character appears in the string. Ex: If the input is: n M Monday the output is: 1 1 Ex: If the input is: z y Today is Monday the output is: 0 2 Ex: If the input is: n y It's a sunny day the output is: 2 2 Case matters. Ex: If the input is: n N Nobody...
2.13 LAB: Driving cost - methods Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and...
2.13 LAB: Driving cost - methods Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the method is called with 50 20.0 3.1599, the method returns 7.89975. Define that method in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your drivingCost() method...
in java A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose...
in java A pedometer treats walking 2,000 steps as walking 1 mile. Write a program whose input is the number of steps, and whose output is the miles walked. Output each floating-point value with two digits after the decimal point, which can be achieved as follows: System.out.printf("%.2f", yourValue); Ex: If the input is: 5345 the output is: 2.67 Your program must define and call a method: public static double stepsToMiles(int userSteps)
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[]) ).
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total...
C Program Write a program to count the frequency of each alphabet letter (A-Z a-z, total 52 case sensitive) and five special characters (‘.’, ‘,’, ‘:’, ‘;’ and ‘!’) in all the .txt files under a given directory. The program should include a header count.h, alphabetcount.c to count the frequency of alphabet letters; and specialcharcount.c to count the frequency of special characters. Please only add code to where it says //ADDCODEHERE and keep function names the same. I have also...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list...
7.6 LAB: Exception handling to detect input String vs. Integer The given program reads a list of single-word first names and ages (ending with -1), and outputs that list with the age incremented. The program fails and throws an exception if the second input on a line is a String rather than an Integer. At FIXME in the code, add a try/catch statement to catch java.util.InputMismatchException, and output 0 for the age. Ex: If the input is: Lee 18 Lua...
Write a program that takes a string of characters (including spaces) as input, computes the frequency...
Write a program that takes a string of characters (including spaces) as input, computes the frequency of each character, sorts them by frequency, and outputs the Huffman code for each character.   When you are writing your program, you should first test it on a string of 7 characters, so you can check it. PLEASE NOTE: Your program must work for any text that has upper and lower case letters digits 0 - 9, commas, periods, and spaces. Please submit the...
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper...
Write a Java program that counts the number of alphabetic ('A' to 'Z' in both upper and lower case) and digit ('1' to '3') characters in a String. The method header is as follows: public static int countNumAlphabetic ( String str ) { } Sample Output: countNumAlphabetic("mA13Th9zB86") returns 8
C++ code please: Write a program to count the number of positive and the number of...
C++ code please: Write a program to count the number of positive and the number of negative inputs values. The program will stop when a 0 is input. Ex: If the input is -5.5 568 2.332 0 the output is 2 positive number(s) and 1 negative number(s). Ex: If the input is 153.0 0.534 2.2 5.6 46.584 0.015 5 0 the output is 7 positive number(s) and 0 negative number(s).
4.16 LAB: Checker for integer string Instructor note: This zyLab counts as a homework grade and...
4.16 LAB: Checker for integer string Instructor note: This zyLab counts as a homework grade and is due by 11:59pm on 9Oct. Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. You may assume that the string does not contain spaces and will always contain less than 50 characters. Ex: If the input is: 1995 the output...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT