replacing charaters a to c , and c to a

hello i need a small cpp to replace all the a to c and c to a

so something like this :" cat can run fast = act acn run fcst"


1
2
string s;
replace (s.begin(),s.end(), 'a','c' );



any thoughts
use a lookup table, for example

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

int main()
{
    std::string s = "cat can run fast";

    std::array<char, std::numeric_limits<char>::max()> xlat;
    std::iota(xlat.begin(), xlat.end(), 0);
    std::swap(xlat['a'], xlat['c']);

    for(char& c: s)
        c = xlat[c];

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

online demo: http://ideone.com/hMMYLO
Last edited on
Topic archived. No new replies allowed.