CC-9 : Implement Replace() functionality.
Description:
Write code to accept input String parameter named str, input String parameter named findStr and input String parameter named replaceStr. Find all instances of findStr in str and replace by string replaceStr. Return the String str with replacements done.
Test cases and expected outputs:
| Input Parameters | Expected outputs |
|---|---|
| str="The String class stores All character strings . All string literals in Java ,
such as "abc", are implemented as Strings. All Strings are immutable ; their
values cannot be changed once they are created. All StringBuffers support changeable strings .As String objects are unchangeable they can be shared with All . "; findStr=”All” replaceStr=”Some” |
The String class stores Some character strings . Some string literals in Java , such as "abc", are implemented as Strings. Some Strings are immutable ; their values cannot be changed once they are created. Some StringBuffers support changeable strings . As String objects are unchangeable they can be shared with Some . |
| str="Earth is part of the Solar system . We live on Earth . "; findStr=”Earth” replaceStr=”Mars” |
Mars is part of the Solar system . We live on Mars . |
| str="Earth is part of the Solar system . We live on Earth . "; findStr=”Star” replaceStr=”Asteroids” |
Earth is part of the Solar system . We live on Earth . |
Pseudocode:
| The java method should accept following input parameters: str (String), find (String), replace (String). |
| Initialize char array chars= str.toCharArray(). |
| Initialize String variable word=””; we will use variable word to iterate through word in str and check if the same match find and need to be replaced by replace. |
| Initialize ArrayList words to hold the reconstructed tokenised/ processed words of str. |
| Iterate through chars using for loop using idx as loop variable, starting from index 0 and incrementing idx till we reach chars.length-1:
If chars[idx] is not a space or end-of-line character we can add it to the current tokenised variable word. This way we will construct a word from each current character till we reach a space character token.
If chars[idx] is a space, its means we have found a complete word:
If word matches find, add replace to ArrayList words.
If word does not match find, add word to ArrayList words.
Add a space token to ArrayList words.
Reset word to “” to start collating the next token word.
If chars[idx] is end-of-line character or if we have reached the last index of chars, we have found the last character of the current line:
If current word matches find, add replace to ArrayList words.
If current word does not match find, add word to ArrayList words.
Add a “\n” token to ArrayList words.
Reset word to “” to start collating the next token word.
|
| At the end of above for loop use String.join(words) method to create the required new String which has the processed replaced words. Return this new String. |
The above algorithm can also be completed using Java String APIs:
| Call String.replace(find, replace) method str. |
| Return str |
Code:
public String stringReplace(String str, String find, String replace) {
char[] chars=str.toCharArray();
String word="";
ArrayList<String> words=new ArrayList<String>();
for (int idx=0; idx<chars.length; idx++) {
if ((chars[idx] !=' ')&& (chars[idx]!= '\n')) {
word=word+chars[idx];
}
if (chars[idx] ==' ') {
if (word.equals(find)) {
words.add(replace);
}else {
words.add(word);
}
words.add(" ");
word="";
}
if ((chars[idx] =='\n') || (idx == chars.length-1)) {
if ((idx-1 >= 0)&& (Character.isWhitespace(chars[idx-1])==false)){
if (word.equals(find)) {
words.add(replace);
}else {
words.add(word);
}
}
words.add(""+'\n');
word="";
//System.out.println("Word Count Per Line: "+wCnt);
}
}
return String.join("", words);
}
public String stringReplaceWithAPI(String str, String find, String replace) {
str=str.replaceAll(find, replace);
return str;
}
Click here to download and run code and test cases !
| About Us | Privacy Policy | Contact us |