How to Find and Replace All Occurrences of a Substring in a C++ String? Last Updated : 05 Feb, 2024 Comments Improve Suggest changes Like Article Like Report In C++, strings are sequences of characters that are used to represent textual data. In this article, we will learn how to find and replace all the occurrences of a particular substring in the given string. For Example, Input: str = "Lokesh is a good programmer, but Lokesh is still in the learning phase." // replaceing 'Lokesh" with "Ram" Output: str = "Ram is a good programmer, but Ram is still in the learning phase."Find and Replace All Occurrences of a Substring in C++ We will divide the process into two parts: first is finding and then replacing. We will use the std::string::find() to find the position of occurrence of the substring in the main string.We will then use the std::string::replace() to replace that part of the string and set the range of find() function after the replaced characters to the end of the main string.We will keep doing that till the find() function returns the std::string::npos which means that no occurrence is found in the given range.C++ Program to Replace All the Occurrence of a Substring C++ // C++ Program to Replace all the occurences Substring in a // String #include <iostream> #include <string> using namespace std; int main() { // Input string string input = "Hi, my name is Lokesh " ". Lokesh means King of the World"; // Substring to find string replace_word = "Lokesh"; // Replacement string string replace_by = "Ram"; // Find the first occurrence of the substring size_t pos = input.find(replace_word); // Iterate through the string and replace all // occurrences while (pos != string::npos) { // Replace the substring with the specified string input.replace(pos, replace_word.size(), replace_by); // Find the next occurrence of the substring pos = input.find(replace_word, pos + replace_by.size()); } // Print the modified string cout << "New String is: " << input << endl; return 0; } OutputNew String is: Hi, my name is Ram . Ram means King of the World Time Complexity: O(N*M), where N is the length of the string and M is the length of the substring.Space Complexity: O(1) Comment More infoAdvertise with us Next Article How to Find and Replace All Occurrences of a Substring in a C++ String? O officialsi8v5f Follow Improve Article Tags : C++ Programs C++ cpp-string CPP Examples Practice Tags : CPP Similar Reads How to Replace All Occurences of an Element in a Set in C++? In C++, a set is an associative container that stores unique elements in a sorted order. In this article, we will learn how to replace all occurrences of a specific element in a set in C++. Example: Input: mySet = {1,2,3,2,4,5,2}; target = 2 replacement = 6 Output: After Replacement: 1 3 4 5 6 Repla 2 min read How to Replace All Occurrences of an Element in a List in C++? In C++, a std::list that represents a doubly linked list is a sequence containers that allow non-contiguous memory allocation. In this article, we will learn how to replace all the occurrences of a specific element in a list using C++ STL. Example: Input: myList = {10, 20, 30, 10, 30, 30, 50}; oldEl 2 min read How to Split a C++ String into a Vector of Substrings? In C++, splitting a string into a vector of substrings means we have to split the given string into a substring based on a given delimiter and store each substring in a vector. In this article, we will learn how to split a string into a vector of substrings in C++. Example: Input: str= "Hello, I am 2 min read Replace all occurrences of character X with character Y in given string Given a string str and two characters X and Y, the task is to write a recursive function to replace all occurrences of character X with character Y. Examples: Input: str = abacd, X = a, Y = x Output: xbxcd Input: str = abacd, X = e, Y = y Output: abacd Iterative Approach: The idea is to iterate over 7 min read How to Replace Second Occurrence of an Element from a Vector? In C++, a vector is a dynamic array that can grow and shrink in size as needed. In this article, we will learn how to replace the second occurrence of a specific element in a vector. Example: Input: myVector = { 5,2,8,5,8,8} Element: 8 Replacement: 10 Output: myVector = { 5,2,8,5,10,8}Replace Second 2 min read Remove all occurrences of a character in a string | Recursive approach Given string str, the task is to write a recursive program to remove all the occurrences of a character X in the string. Examples: Input: str = "geeksforgeeks", c = 'e' Output: gksforgksInput: str = "geeksforgeeks", c = 'g' Output: eeksforeeks Iterative Approach: The iterative approach to this probl 5 min read How to Find the Length of a Substring in a char Array? In C++, a substring is a string inside another string that is the contiguous part of the string. In this article, we will learn how to find the length of a substring in a char array in C++. Example Input: str[] = "Hello, World!";substr= "World";Output: Length of substring World is: 5Finding Length o 2 min read C++ Program To Check If A String Is Substring Of Another Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output 4 min read Find if a String ends With the Given Substring in C++ You are given two strings. The task is to check if the main string ends with another string in C++. Example Input: mainString = "Hello! Geek"; targetString = "Geek" Output: Hello! Geek ends with Geek.We can find if the given string ends with the target string using the following method: Checking if 2 min read How to Replace Text in a String Using Regex in C++? Regular expressions or what we can call regex for short are sequences of symbols and characters that create a search pattern and help us to find specific patterns within a given text. In this article, we will learn how to replace text in a string using regex in C++. Example Input: str = "Hello, Worl 2 min read Like