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
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Replace all occurrences of string AB with C without using extra space
- Replace all occurrences of pi with 3.14 in a 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
- Count occurrences of a string that can be constructed from another given string
- Count of occurrences of a "1(0+)1" pattern in a string
- Remove all occurrences of a character in a string
- Count occurrences of a word in string
- Count occurrences of a character in a repeated string
- Check if a binary string has two consecutive occurrences of one everywhere
- Rearrange a binary string as alternate x and y occurrences
- Replace a character c1 with c2 and c2 with c1 in a string S
- Replace two substrings (of a string) with each other
- Remove duplicates from string keeping the order according to last occurrences
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.