C++ Program to Replace a Character in a String
Last Updated :
11 Apr, 2023
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;
}
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:
- Define a function replaceChar that takes a string S and two characters c1 and c2 as input.
- Initialize two temporary strings s1 and s2 as empty strings.
- Iterate over the characters in the input string S.
- For each character c in S, check if c is equal to c1 or c2.
- If c is equal to c1, append c2 to s1 and c1 to s2.
- If c is equal to c2, append c1 to s1 and c2 to s2.
- If c is neither equal to c1 nor c2, append c to both s1 and s2.
- 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;
}
OutputModified string: Omkhar
Time Complexity: O(n), where n is the length of the input string.
Auxiliary Space: O(n).
Similar Reads
C++ Program to Swap characters in a String Given a String S of length N, two integers B and C, the task is to traverse characters starting from the beginning, swapping a character with the character after C places from it, i.e. swap characters at position i and (i + C)%N. Repeat this process B times, advancing one position at a time. Your ta
6 min read
C Program to Extract Characters From a String Character extraction can be done by iterating through the string in the form of a character array. It basically means plucking out a certain amount of characters from an array or a string. Now, to take input in C we do it by using the following methods: scanf("%c",&str[i]); - Using a loopscanf("
2 min read
Convert character array to string in C++ This article shows how to convert a character array to a string in C++. The std::string in c++ has a lot of inbuilt functions which makes implementation much easier than handling a character array. Hence, it would often be easier to work if we convert a character array to string. Examples: Input: ch
4 min read
C++ Program To Reverse Words In A Given String Example: Let the input string be "i like this program very much". The function should change the string to "much very program this like i" Examples: Input: s = "geeks quiz practice code" Output: s = "code practice quiz geeks" Input: s = "getting good at coding needs a lot of practice" Output: s = "p
7 min read
How to Remove Last Character From C++ String? In C++, strings are stored as the std::string class objects. In this article, we will look at how to remove the last character from this C++ string object. For Example, Input: Hello! GeeksOutput: Hello! GeekRemove the Last Character from a String in C++To remove the last character from a string, we
2 min read