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...

Recommended Answers

All 11 Replies

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.

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.

Member Avatar for hfx642

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

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(..)

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.

I agree on that... considering efficiency and possibly longs strings...

Member Avatar for hfx642

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()

Member Avatar for hfx642

Okay, Okay.
Don't crap on me because I'm agreeing with you!
Geez!

I'm not, I just didn't understand your post.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.