Replace in String
Many times, it is very important to make corrections in strings. Replacing a character, word or phrase in a String is another very common operation performed on Strings.
The simplest approach to solve the given problem is to traverse the string S and when any string S1 is found as a substring in the string S then replace it by S2. Follow the steps below to solve this problem:
- Initialize a string ans to store the resultant string after replacing all the occurrences of the substring S1 to S2 in the string S.
- Iterate over the characters of the string S using variable i and perform the following steps:
- If the prefix substring of the string S is equal to S1 from the index i, then add the string S2 in the string ans.
- Otherwise, add the current character to the string ans.
- After completing the above steps, print the string ans as the result.
Contact Us