Open In App

C++ Program to Replace a Character in a String

Improve
Improve
Like Article
Like
Save
Share
Report

Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. 
Examples:

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

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

Traverse through the string and check for the occurrences of c1 and c2. If c1 is found then replace it with c2 and else if c2 is found replace it with c1.

C++




// C++ program to replace c1 with c2
// and c2 with c1
#include <bits/stdc++.h>
using namespace std;
string replace(string s,
               char c1, char c2)
{
    int l = s.length();
 
    // loop to traverse in the string
    for (int i = 0; i < l; i++)
    {
        // check for c1 and replace
        if (s[i] == c1)
            s[i] = c2;
 
        // check for c2 and replace
        else if (s[i] == c2)
            s[i] = c1;
    }
    return s;
}
 
// Driver code
int main()
{
    string s = "grrksfoegrrks";
    char c1 = 'e', c2 = 'r';
    cout << replace(s, c1, c2);
    return 0;
}


Output

geeksforgeeks

Time Complexity: O(n) 
Auxiliary Space: O(n), because the program creates a copy of string s.

Two-Temporary-String Character Replacement

The approach involves iterating over the input string character by character, and replacing the characters as required. For each character, we check if it is equal to c1 or c2, and replace it with the other character if needed. We use two temporary strings to keep track of the replacement characters, and finally update the input string with the modified string. This approach involves a single pass over the input string, and hence has a time complexity of O(n), where n is the length of the input string.

Steps:

  1. Define a function replaceChar that takes a string S and two characters c1 and c2 as input.
  2. Initialize two temporary strings s1 and s2 as empty strings.
  3. Iterate over the characters in the input string S.
  4. For each character c in S, check if c is equal to c1 or c2.
  5. If c is equal to c1, append c2 to s1 and c1 to s2.
  6. If c is equal to c2, append c1 to s1 and c2 to s2.
  7. If c is neither equal to c1 nor c2, append c to both s1 and s2.
  8. Update the input string S with s1.

C++




#include <iostream>
#include <string>
 
using namespace std;
 
void replaceChar(string& S, char c1, char c2) {
    string s1, s2;
    for (char c : S) {
        if (c == c1) {
            s1 += c2;
            s2 += c1;
        } else if (c == c2) {
            s1 += c1;
            s2 += c2;
        } else {
            s1 += c;
            s2 += c;
        }
    }
    S = s1;
}
 
int main() {
    string S = "Omkhaz";
    char c1 = 'z', c2 = 'r';
 
    // Replace characters in string
    replaceChar(S, c1, c2);
 
    // Print modified string
    cout << "Modified string: " << S << endl;
 
    return 0;
}


Output

Modified string: Omkhar

Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(n).



Last Updated : 11 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads