Given a string and a substring, the task is to replace all occurrences of the substring with space. We also need to remove trailing and leading spaces created due to this.
Input: str = “LIELIEILIEAMLIECOOL”, sub = “LIE”
Output: I AM COOL
By replacing all occurrences of Sub in Str with empty spaces, we extract the secret message as I AM COOL.
Input: str = “XYZAXYZBXYZC”, sub = “XYZ”
By replacing all occurrences of Sub in Str with empty spaces, we extract the secret message as ABC.
- In the given string Str, replace all occurrences of Sub with empty spaces.
- Remove unwanted empty spaces in start and end of the string.
- Print the modified string.
Below is the implementation of the above approach:
I AM COOL
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Replace all occurrences of string AB with C without using extra space
- Replace all occurrences of pi with 3.14 in a given string
- Replace all occurrences of character X with character Y in given string
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- Program to replace every space in a string with hyphen
- In-place replace multiple occurrences of a pattern
- Smallest string obtained by removing all occurrences of 01 and 11 from Binary String
- Remove all occurrences of a character in a string
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Map every character of one string to another such that all occurrences are mapped to the same character
- Swap all occurrences of two characters to get lexicographically smallest string
- Remove all occurrences of a character in a string | Recursive approach
- Sum of all distances between occurrences of same characters in a given string
- Count occurrences of a string that can be constructed from another given string
- Replace all consonants with nearest vowels in a string
- Minimize cost to replace all the vowels of a given String by a single vowel
- Sum of all substrings of a string representing a number | Set 2 (Constant Extra Space)
- Group all occurrences of characters according to first appearance
- Check if all occurrences of a character appear together
- Minimize ASCII values sum after removing all occurrences of one character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.