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
std::string::replace() in C++ The string::replace() function in C++ is used to replace a single or multiple characters from a given index. It is the member function of std::string class. In this article, we will learn how to use the string::replace() function in our C++ program.SyntaxThe string::replace() function provides 6 dif
6 min read
std::string::replace_copy(), std::string::replace_copy_if in C++ replace_copy replace_copy() is a combination of copy() and replace(). It copies the elements in the range [first, last) to the range beginning at result, replacing the appearances of old_value by new_value.The range copied is [first, last), which contains all the elements between first and last, inc
4 min read
Different ways to copy a string in C/C++ Copying a string is a common operation in C/C++ used to create a duplicate copy of the original string. In this article, we will see how to copy strings in C/C++. Methods to Copy a String in C/C++1. Using strcpy() We can use the inbuilt function strcpy() from <string.h> header file to copy one
15+ min read
How to write long strings in Multi-lines C/C++? Image a situation where we want to use or print a long long string in C or C++, how to do this? In C/C++, we can break a string at any point in the middle using two double quotes in the middle. Below is a simple example to demonstrate the same. C #include<stdio.h> int main() { // We can put tw
2 min read
std::string::insert() in C++ In C++, the string insert() function is used to insert characters or a string at the given position of the string. For example,C++#include <bits/stdc++.h> using namespace std; int main() { string s = "Geeks"; // Inserting another string at the friend // of s s.insert(s.size(), "forGeeks"); cou
4 min read
strncat() function in C/C++ In C/C++, strncat() is a predefined function used for string handling. string.h is the header file required for string functions.This function appends not more than n characters from the string pointed to by src to the end of the string pointed to by dest plus a terminating Null-character. The initi
4 min read