C++ Program to Replace a Character in a String

Last Updated : 26 Aug, 2026

Given a string and two characters c1 and c2, replace every occurrence of c1 with c2 and every occurrence of c2 with c1. There are two approaches to perform the replacement while traversing the string.

  • The first approach modifies the original string directly.
  • The second approach uses temporary strings to construct the modified result.

Examples:

Input: grrksfoegrrks,
c1 = e, c2 = r
Output: geeksforgeeks

Input: ratul,
c1 = t, c2 = h
Output: rahul

Approaches to Replace Characters in a String

We can replace the characters using either a direct in-place approach or a temporary-string approach, depending on whether we want to modify the original string or create the result separately.

Approach 1: Replace Characters In-Place

In this approach, the original string is modified directly while traversing it. No additional string is created.

  • Traverse each character of the string.
  • Replace c1 with c2 and c2 with c1.
  • Store the changes directly in the original string.

The replacement is performed in-place.

C++
#include <iostream>
#include <string>
using namespace std;

void replaceCharacters(string& s, char c1, char c2)
{
    for (char& ch : s) {
        if (ch == c1)
            ch = c2;
        else if (ch == c2)
            ch = c1;
    }
}

int main()
{
    string s = "grrksfoegrrks";
    char c1 = 'e';
    char c2 = 'r';

    replaceCharacters(s, c1, c2);

    cout << s;

    return 0;
} 

Output
geeksforgeeks

Explanation

  • The string is passed by reference, so changes are made directly to the original string.
  • The range-based for loop visits each character.
  • c1 is replaced with c2, and c2 is replaced with c1.
  • Since each character is processed only once, the approach is efficient.

Approach 2: Replace Characters Using Temporary Strings

In this approach, the original string is not modified during traversal. A separate string is created to store the replaced characters.

  • Traverse each character of the original string.
  • Append the corresponding replacement character to the new string.
  • Assign the resulting string back to the original string.

The replacement is performed by building a new string.

C++
#include <iostream>
#include <string>
using namespace std;

void replaceCharacters(string& s, char c1, char c2)
{
    string result;

    for (char ch : s) {
        if (ch == c1)
            result += c2;
        else if (ch == c2)
            result += c1;
        else
            result += ch;
    }

    s = result;
}

int main()
{
    string s = "Omkhaz";
    char c1 = 'z';
    char c2 = 'r';

    replaceCharacters(s, c1, c2);

    cout << "Modified string: " << s;

    return 0;
} 

Output
Modified string: Omkhar

Explanation

  • result stores the modified version of the string.
  • Each character is checked during a single traversal.
  • The corresponding replacement character is appended to result.
  • Characters that do not match c1 or c2 are copied unchanged.
  • The original string is updated after the traversal.
Comment