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?

stack_
  • 35
  • 2
  • 7
  • 3
    The answer is yes. You will need to either truncate one string, or extend the other string. – Eljay Aug 24 '18 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. – Max Langhof Aug 24 '18 at 11:55
  • 1
    You can define the `^` operator however you want to (see [operator overloading](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading)). What type of behavior are you looking for when the lengths of the strings are unequal? – Ben Jones Aug 24 '18 at 12:13
  • but string str1 length is not fixed , it varies every time – stack_ Aug 24 '18 at 12:14
  • hi Ben , My second string is static and first string changing every time , So My need is to xor it – stack_ Aug 24 '18 at 12:20
  • 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? – Ben Jones Aug 24 '18 at 12:22
  • You probably have to pad the shortest one with 0s in the most significant position. – drescherjm Aug 24 '18 at 12:23
  • You have yet to define what "it" is, in your mind. You cannot solve a problem before you know what it is. – Lightness Races in Orbit Aug 24 '18 at 14:44

2 Answers2

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
Richard Hodges
  • 68,278
  • 7
  • 90
  • 142
  • Thank Richard , but I want to store the xoring result in `std::string` – stack_ Aug 24 '18 at 13:06
  • 2
    @stack_ `result` is a `std::string` – Richard Hodges Aug 24 '18 at 13:07
  • @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. – drescherjm Aug 24 '18 at 13:51
  • @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_ Aug 25 '18 at 11:51
  • @Richard , Does it need to use `bitset` if the length of two strings is equal ? And If is is not needed then How will I Xor the same length strings? – stack_ Aug 25 '18 at 12:10
  • 1
    @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. – Richard Hodges Aug 25 '18 at 17:28
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.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • 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` – stack_ Aug 25 '18 at 11:44
  • Is It possible ?And if possible then what should I do? – stack_ Aug 25 '18 at 11:51