Complete method called characterCount below so that it returns the number of times the character ch appears in the string s.

public int characterCount(String s, char ch)
{
}

I know that I have to use a for loop and use s.length() as a stopping point. Other than that, I'm stuck...

posting empty methods as your attempt will not get you any replies. please try something, and then post the code if it doesn't work. If you are stuck look through the api, and see what you can find.

Edited 5 Years Ago by sirlink99: n/a

not to mention the fact that, in the last 24 hours alone, code snippets have been posted on this forum that could easily help you solve your problem.
this is not a "code-delivery" that works 24/7, a bit effort on your part is expected.

A "for loop" and the "substring()" method, are all you need.
Assembly required!

I would throw an indexOf(..) in there too.

I would use charAt(..)

that would make you iterate over each individual character.
what if it's a String with a length of 1000 with only one occurence of the char?
indexOf() and substring would be more efficient to say the least.

that would make you iterate over each individual character.
what if it's a String with a length of 1000 with only one occurence of the char?
indexOf() and substring would be more efficient to say the least.

Sorry... Brain Fart!
indexOf() is definitely the way to go.
(I think that I was getting THIS posting confused with a different posting.)

indexOf() is definitely the way to go.

isn't that what I said? my post was a reaction on a post of stevanity in which he suggested to use charAt(), not an argument against indexOf()

This article has been dead for over six months. Start a new discussion instead.