> For the complete documentation index, see [llms.txt](https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://abhyas-kanaujia.gitbook.io/lb-dsa-notes-and-homework-abhyas/14-character-array-and-strings/discord-homework/07.-remove-all-occurrences-of-a-substring.md).

# 07. Remove All Occurrences of a Substring

### Remove Spaces

[Link](https://bit.ly/3sfP71Q)

```cpp
string replaceSpaces(string &str){
  int pos = string::npos;

  while((pos = str.find(' ')) != string::npos) 
    str.replace(pos, 1, "@40");
  
  return str;
}
```

### Remove Substring

[Link](https://leetcode.com/problems/remove-all-occurrences-of-a-substring/)

```cpp
string removeOccurrences(string s, string part) {
    int pos = string::npos;
    while((pos = s.find(part)) != string::npos) 
        s.erase(pos, part.length());
    return s;
}
```
