Java Replace String Function

Problem

Write a Java method to replace a sub string in a given text with another string of the same size

Solution

Loop through the text character by character. Starting at the current character loop a number of times equal to the size of the sub string while comparing characters in the original text and the sub string that we are looking for. If a match is found replace it with the new string. If the sub string and the new string are not of the same size then it needs special handling but in this question we are assuming they are of the same size

Code

Here is the code in Java

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *