• Post Reply Bookmark Topic Watch Topic
  • New Topic

Converting specific characters in a string

 
Bob Lomax
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a program description:
1)user enters a string: "Java Rules"
2)then enters a string containing
characters to be converted: "ae s" (characters in this case are 'a' 'e' '<space>' and 's')
3)user then enters an output character that the characters to be converted will be converted to:"$"
therefore the string "Java Rules" will become "J$v$$Rul$$"
can anyone suggest an efficient algorithm to deal with this problem?
 
Ganesh Ram
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BitSet replaceChars
replaceChars = new BitSet(256);
replaceChars.set('a');
replaceChars.set('b');
replaceCh........
String s = "your string";
for (int i = 0; i < s.length(); i++) {
int c = (int)s.charAt(i);
if (replaceChars.get(c)) {
put in your custom char.
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!