replacing a character in a string

Hi
How can i replace a character from a introduced string?
let's say i introduce "aabcf"
i want to replace all a leters with b , so i introduce from the keyboard, that i want to replace a with b, what can i do next?
Here is my attempt :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string x;
    char y, t;
    int i;
    cout<<"The string is :  ";
    cin>>x;
    cout<< "The desired character is : ";
    cin>> x.at(i);
    for (i=0; i<x.length(); i++)
    {
        cin>> x.at(i);
    }
    cout<< "With what letter do you want to replace it : ";
    cin>>y;
    for (i=0; i<x.length(); i++)
    {
       t = x.at(i) == y;
    }
    cout<<t;

    return 0;
} 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
	string x;
	char y {}, d {};

	cout << "The string is: ";
	cin >> x;

	cout << "The desired character is: ";
	cin >> d;

	cout << "With what letter do you want to replace it: ";
	cin >> y;

	std::replace_if(x.begin(), x.end(), [&d](char c) {return d == c; }, y);

	cout << x;
}



The string is: aabcf
The desired character is: a
With what letter do you want to replace it: b
bbbcf

thanks
but if i want to replace just the characters "a" with b, and "b" with a, the other letters to remain the same?
Only the specified desired character is replaced with the specified replacement letter. The others remain the same and are not changed.


Do you mean you want to do multiple replacements - eg c with d and e with h and z with y allwithin the same operation?
i mean if i introduce "aabbtt" to return "bbaatt"
That is a multiple replacement - a with b, b with a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
include <string>
#include <iostream>
#include <map>

int main()
{
	std::map<char, char> repl;

	do {
		char oldc {}, newc {}, more {};

		std::cout << "Enter old char: ";
		std::cin >> oldc;

		std::cout << "Enter new char: ";
		std::cin >> newc;

		repl[oldc] = newc;

		std::cout << "More? (y/n): ";
		std::cin >> more;

		if (more != 'y')
			break;

	} while (true);

	std::string str;

	std::cout << "Enter string: ";
	std::cin >> str;

	for (auto& c : str)
		if (const auto f = repl.find(c); f != repl.end())
			c = f->second;

	std::cout << str << '\n';
}



Enter old char: a
Enter new char: b
More? (y/n): y
Enter old char: b
Enter new char: a
More? (y/n): n
Enter string: aabbtt
bbaatt

but without enter old char and new char, just enter string?
Last edited on
OK

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <string>
#include <iostream>
#include <map>

int main()
{
	const std::map<char, char> repl {{'a', 'b'}, {'b', 'a'}};

	std::string str;

	std::cout << "Enter string: ";
	std::cin >> str;

	for (auto& c : str)
		if (const auto f = repl.find(c); f != repl.end())
			c = f->second;

	std::cout << str << '\n';
}

Topic archived. No new replies allowed.