Replace a sub-string in a string in C++

Understanding String Manipulation

Replacing a sub-string in a string means finding a specific sequence of characters and replacing it with another sequence.

We will explore three different methods to achieve this in C++.

Method 1: Using Loop

This method iterates through the string and replaces occurrences of the sub-string.

#include <iostream>
#include <algorithm>
using namespace std;

void replaceSubstring(string &str, const string &oldWord, const string &newWord) {
    size_t pos = 0;
    while ((pos = str.find(oldWord, pos)) != string::npos) {
        str.replace(pos, oldWord.length(), newWord);
        pos += newWord.length();
    }
}

int main() {
    string str = "Hello World!";
    replaceSubstring(str, "World", "C++");
    cout << "Modified String: " << str;
    return 0;
}
            
Input: Hello World!
Output: Hello C++!

Method 2: Using Recursion

This method replaces the sub-string recursively.

#include <iostream>
#include <algorithm>
using namespace std;

void replaceRecursive(string &str, const string &oldWord, const string &newWord) {
    size_t pos = str.find(oldWord);
    if (pos == string::npos) return;
    str.replace(pos, oldWord.length(), newWord);
    replaceRecursive(str, oldWord, newWord);
}

int main() {
    string str = "I love programming";
    replaceRecursive(str, "love", "enjoy");
    cout << "Modified String: " << str;
    return 0;
}
            
Input: I love programming
Output: I enjoy programming

Method 3: Using String Functions

This method uses C++'s built-in find() and replace() functions.

#include <iostream>
#include <algorithm>
using namespace std;

void replaceUsingFunctions(string &str, const string &oldWord, const string &newWord) {
    size_t pos = str.find(oldWord);
    if (pos != string::npos) {
        str.replace(pos, oldWord.length(), newWord);
    }
}

int main() {
    string str = "Replace old text";
    replaceUsingFunctions(str, "old", "new");
    cout << "Modified String: " << str;
    return 0;
}
            
Input: Replace old text
Output: Replace new text
Strings

Below You will find some of the most important codes in languages like C, C++, Java, and Python. These codes are of prime importance for college semester exams and online tests.

Getting Started

Check whether a character is a vowel or consonant: C C++ Java Python

Check whether a character is an alphabet or not: C C++ Java Python

Find the ASCII value of a character: C C++ Java Python

Length of the string without using strlen() function: C C++ Java Python

Toggle each character in a string: C C++ Java Python

Count the number of vowels: C C++ Java Python

Remove the vowels from a string: C C++ Java Python

Check if the given string is Palindrome or not: C C++ Java Python

Print the given string in reverse order: C C++ Java Python

Remove all characters from string except alphabets: C C++ Java Python

Remove spaces from a string: C C++ Java Python

Replace a sub-string in a string: C C++ Java Python

Count common sub-sequences in two strings: C C++ Java Python

Compare two strings with wildcard support in one of them: C C++ Java Python

List all permutations of a given string in dictionary order: C C++ Java Python

Operations on Strings: C C++ Java Python