0

I have two strings

Input

string a="101";
string b="(_^_)v_";

Output

string c="(1^0)v1";

Can you guys please tell me the way to solve it in C++?

Aqeel iqbal
  • 515
  • 5
  • 18
  • You need to give us a lot more details and fix the title of your question. There is no boolean expression here, but what looks like a bitvise logical operation. You need to explain what the rules are for merging the string, and show what you have tried to do and where it fails. – Lev M. Dec 22 '20 at 14:40
  • These aren’t valid strings in C++ – C and C++ both require double quotes [`"..."`] for strings ([ref](https://en.cppreference.com/w/cpp/language/string_literal)) – MTCoster Dec 22 '20 at 14:41
  • Tis `'101'` is not near a string. You probably mean `"101"`. Same for the next line. – Yunnosch Dec 22 '20 at 14:41
  • It is not clear to me. No inbuilt C++ mechanism seems close to what you describe. What your are want in the end is taking each of the ":_" and replacing it with consecutive letters from `a`, right? You probably have to just program it yourself. Or try, show what you have and explain how it fails to get help with fixing it. – Yunnosch Dec 22 '20 at 14:44

1 Answers1

2

For example, so:

#include <iostream>
#include <string>

int main ()
{
    std::string a="101";
    std::string b="(_^_)v_";    

    for(char& c: a)       
        b.replace(b.find("_"), sizeof("_") - 1, std::string(1, c));

    std::cout << b;
}
(1^0)v1

Update: O(n) variant

#include <iostream>
#include <string>

int main()
{
    std::string a="101";
    std::string b="(_^_)v_";    

    for(std::string::iterator ia = a.begin(), ib = b.begin(); ia != a.end() && ib != b.end(); ++ib) {
        if (*ib == '_') {
            *ib = *ia;
            ia++;
        }
    }

    std::cout << b;
}
alex_noname
  • 26,459
  • 5
  • 69
  • 86