+ 1

Transformation

If a symbol is 'A' and next is 'B', transform two symbols in 'C' Input: ABHJKAB Output: CHJKC

15th Apr 2018, 11:50 AM
Maria
Maria - avatar
4 Answers
+ 6
#include <iostream> #include <string> #include <regex> std::string new_str(std::string o) { return std::regex_replace(o, std::regex("AB"), "C"); } int main() { std::cout << new_str("ABHJKAB"); return 0; }
15th Apr 2018, 12:04 PM
Hatsy Rei
Hatsy Rei - avatar
+ 1
@Hatsy Rei, can I do two replacements using regex_replace? For example I need to transform not only "AB" but and "BA" too
15th Apr 2018, 12:57 PM
Maria
Maria - avatar
+ 1
and if I have "ABABA" I must get "CCA"
15th Apr 2018, 1:01 PM
Maria
Maria - avatar
+ 1
oh I got
15th Apr 2018, 1:47 PM
Maria
Maria - avatar