File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes occurence of char in a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "occurence of char in a string" Watch "occurence of char in a string" New topic

occurence of char in a string

suman deb
Ranch Hand

Joined: Jul 14, 2005
Posts: 54
hi all,

how to find out the occurence of each char in a string? e.g.

for the string "HELLO" , the required out put is--

H - 1
E - 1
L - 2
o - 1

please help
Christophe Verré

Joined: Nov 24, 2005
Posts: 14688

You could try with String.indexOf(int ch, int fromIndex), looping through the whole String.

[My Blog]
All roads lead to JavaRanch
Allan Morris

Joined: Sep 03, 2006
Posts: 4
Hi, a map is a good data structure, pairing each character to a count. You can do it like this using the Collections framework:

[ September 05, 2006: Message edited by: Allan Morris ]
I agree. Here's the link:
subject: occurence of char in a string
It's not a secret anymore!