Replacing letters

So i want to replace letters to adaptate my letter language, i made a program but when it replaces letters it replaces letters multiple times for example :

when i write "oro"
the output is "araataara"
it should be "ata"

1
2
3
4
5
6
7
8
if(letterTR.find('t') != string::npos) 
{
std::replace(letterTR.begin(), letterTR.end() ,'t', 'r'); cout << letterTR; 
} 
if(letterTR.find('r') != string::npos) 
{
std::replace(letterTR.begin(), letterTR.end() ,'r', 't'); cout << letterTR; 
} 


how can i prevent it?
Last edited on
Your code does not match the problem. Please provide a minimal compilable code (including main) that shows this problem.

It looks like you replace 'o' with 'a'
'r' with 't'
't' back to 'r'

Each time you output the result, hence you have three times that output.
So reduce the output to one at the end and remove the unnecessary replace.
if i don't replace 't back to r' or 'o back to a', t and o won't be replaced, how can i fix that
In your code you do not replace 'o' with 'a'. It would look like:
1
2
3
std::replace(letterTR.begin(), letterTR.end() ,'t', 'r');
std::replace(letterTR.begin(), letterTR.end() ,'a', 'o');
cout << letterTR;
now it doesn't replaces some letters and only first word is being replaced...
Last edited on
for example,
r isn't replacing with t, only t is being replaced with r

when i write "oro oco" it only replaces "oro"
Consider where the repl array specifies the chars to be replaced with the new ones:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <algorithm>
#include <string>
#include <iostream>

struct Repl {
	char org {};
	char rep {};
};

int main()
{
	const Repl repl[] {{ 'o', 'a' }, {'r', 't'}};

	std::string inp;
	std::cout << "Enter sentence: ";
	std::getline(std::cin, inp);

	std::transform(inp.begin(), inp.end(), inp.begin(), [&repl](char ch) {
		const auto it {std::find_if(std::begin(repl), std::end(repl), [ch](const auto& elem) { return elem.org == ch; })};
		return it != std::end(repl) ? it->rep : ch;
	});

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

Last edited on
r isn't replacing with t, only t is being replaced with r
So it is not quite clear what replacements you want. If you want to replace 'r' with 't' and 't' with 'r' in the same string you can't do it like this.

Constider using a loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for(char& ch : letterTR)
{
  switch(ch)
  {
    case 'r';
      ch = 't';
      break;
    case 't';
      ch = 'r';
      break;
    case 'o';
      ch = 'a';
      break;
  }
}
This way you can replace any letter with any other letter.
if you are using char, and there is more to it than 10 or so letters, you can also make a lookup table.
char tbl[256];
for(int i = 0; i < 256; i++)
tbl[i] = i; //can use iota and fancy stuff here instead.
ok, now tbl is the ascii table (extended).
now make the changes you want one time:
tbl['r'] = 't';
tbl['t'] = 'r';
...
for all the letters in your string
string[index] = tbl[string[index]]; //inefficient, it replaces say e with e if you didn't change e to something else, but it keeps the code smaller/ simpler.
Last edited on
Topic archived. No new replies allowed.