Write code to design your algorithm to replace Chars inside a string
//package com.book2s; public class Main { public static void main(String[] argv) { String str = "book2s.com"; String searchChars = "o"; String replaceChars = "O"; System.out.println(replaceChars(str, searchChars, replaceChars)); }//from ww w .j av a2 s . c o m public static String replaceChars(String str, String searchChars, String replaceChars) { if (isEmpty(str) || isEmpty(searchChars)) return str; if (replaceChars == null) replaceChars = ""; boolean modified = false; int replaceCharsLength = replaceChars.length(); int strLength = str.length(); StringBuffer buf = new StringBuffer(strLength); for (int i = 0; i < strLength; i++) { char ch = str.charAt(i); int index = searchChars.indexOf(ch); if (index >= 0) { modified = true; if (index < replaceCharsLength) buf.append(replaceChars.charAt(index)); } else { buf.append(ch); } } if (modified) return buf.toString(); else return str; } public static boolean isEmpty(String str) { return str == null || str.length() == 0; } public static int indexOf(String str, char searchChar, int startPos) { if (isEmpty(str)) return -1; else return str.indexOf(searchChar, startPos); } }