0

I have two different length c++ strings.

string str1 = "01001110 01100001 01101101 01100101"

string str2 = "00000000 00000000 00000011"

I need to xor str1 and str2 and store the result in a new string

string xor_string =  str1 ^ str2

So, is it possible to xor two different length strings, and to store it into another string ?

I have searched for it but I didn't get it.

Can anyone tell me, how to do it?

8
  • 3
    The answer is yes. You will need to either truncate one string, or extend the other string. Commented Aug 24, 2018 at 11:49
  • 1
    Note that the xor operation is not defined on strings (because they usually contain other characters than just zeros and ones), you will have to define it yourself. Commented Aug 24, 2018 at 11:55
  • 1
    You can define the ^ operator however you want to (see operator overloading). What type of behavior are you looking for when the lengths of the strings are unequal? Commented Aug 24, 2018 at 12:13
  • but string str1 length is not fixed , it varies every time Commented Aug 24, 2018 at 12:14
  • 1
    I understand that the two strings can vary in length. My question is what do you expect the value of xor_string to be? Commented Aug 24, 2018 at 12:22

2 Answers 2

5

Something like this:

#include <string>
#include <iostream>
#include <bitset>
#include <algorithm>

std::bitset<32> to_bitset(std::string s)
{
    auto binary = [](char c) { return c == '0' || c == '1'; };
    auto not_binary = [binary](char c) { return !binary(c); };

    s.erase(std::remove_if(begin(s), end(s), not_binary), end(s));

    return std::bitset<32>(s);
}

std::string to_string(std::bitset<32> bs)
{
    return bs.to_string();
}

int main()
{
    std::string str1 = "01001110 01100001 01101101 01100101";
    std::string str2 = "00000000 00000000 00000011";

    auto result = to_string(to_bitset(str1) ^ to_bitset(str2));

    std::cout << result << std::endl;
}

expected output:

01001110011000010110110101100110
Sign up to request clarification or add additional context in comments.

6 Comments

Thank Richard , but I want to store the xoring result in std::string
@stack_ result is a std::string
@stack_ if your not permitted to use std::bitset I recommend you opening a new question (since this one is answered and this new information would invalidate the good answer) and adding your implementation of operator^ and explain the problem you are having with your code.
@Richard Now I want the original string back which is str1 in my case` and str2 is a static string. Means I want to make std::string result = std::string str1 Is It possible ?And if possible then what should I do? Now I have kept My both string are of equal length = 64
@stack_ it might be easier if you write the function declaration, include comments on what the function should output given certain inputs and provide preconditions (e.g. strings will contain 0,1 and space only, will contain max X digits and so on). Then we can fill in the function definition so it conforms to the requirements. At the moment I am confused. The code I have given you solves the problem you described in the OP.
|
1

You could always walk through or iterate through the string, and place the result into a new string.

std::string str1 = "01001110 01100001 01101101 01100101";
std::string str2 = "00000000 00000000 00000011";
//...  
std::string result;
const char c = ((str1[i] - '0') ^ (str2[i] - '0')) + '0';
result += c;

The fundamental issue is that you need to make the strings of equal length or change your algorithm to handle strings of different length.

For example, do you repeat str2 when str1 is longer or do you prefix str2 with '0'? This you will need to answer for yourself.

2 Comments

Thank you @Thomas . As you told that to make strings of equal length, I will kept the two string same length .` Now I want the original string back which is str1 in my case` and str2 is a static string. Means I want to make std::string result = std::string str1
Is It possible ?And if possible then what should I do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.