• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to find different characters in string ?  RSS feed

 
sreenath gadi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
like i have String s="11222ddeefs"

so here i want program output like 1=2
2=3
d=2
e=2
f=1
and s=1
it has to show no of duplicates in each character in string
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sreenath gadi wrote:like i have String s="11222ddeefs"
so here i want program output like
...
it has to show no of duplicates in each character in string

Have a look at String.charAt(). That should get you started (I presume you know how to write a for loop).

Winston
 
sreenath gadi
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String.charAt() returns all the characters . but how do we divide each character like above output
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difficulty there? You have to iterate each character and if the character matches, remove it from that position, increment the counter for that character.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sreenath gadi wrote: String.charAt() returns all the characters

No it doesn't. It returns ONE character each time it's called; and unless there is a pattern for the entire String (or some "recognisable" part) that you want to match, it's the only way to do what you want.

Don't complicate things, sreenath. Either you want to match a character or count them, or you want something "more". If it's the latter, then you need to explain EXACTLY what you want.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!