Question

Java. Implement the countNotMatching method below, which takes as input a string and a character and...

Java.

Implement the countNotMatching method below, which takes as input a string and a character and returns how many characters in the string are NOT the supplied character. An example main method, as well as corresponding output, is supplied – you cannot change this method, and the output when the program is run must conform exactly to the sample output.

/* sample run:

Other characters: 1

Other characters: 3

Other characters: 3

*/

public static void main(String args[]) {

   System.out.printf("Other characters: %d%n", countNotMatching("Foo", 'o'));

   System.out.printf("Other characters: %d%n", countNotMatching("Bar", 'b'));

   System.out.printf("Other characters: %d%n", countNotMatching("Baz!", 'B'));

}

// put your countNotMatching method here

0 0
Add a comment Improve this question Transcribed image text
Answer #1

public class CountNotMatching {

    /* sample run:
    Other characters: 1
    Other characters: 3
    Other characters: 3
*/
    public static void main(String args[]) {
        System.out.printf("Other characters: %d%n", countNotMatching("Foo", 'o'));
        System.out.printf("Other characters: %d%n", countNotMatching("Bar", 'b'));
        System.out.printf("Other characters: %d%n", countNotMatching("Baz!", 'B'));
    }

    public static int countNotMatching(String s, char ch) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) != ch) {
                ++count;
            }
        }
        return count;
    }
// put your countNotMatching method here
}

Add a comment
Know the answer?
Add Answer to:
Java. Implement the countNotMatching method below, which takes as input a string and a character and...
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
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