Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

#include <iostream>

#include<string>
using namespace std;
//-----End of headers-----

//-----Add new functions here(if any)-----

//-----New functions end here-----

/* Question : Function to convert a given binary string to


gray code string as explained in the problem description. */
string binarytoGray(string binary) {
// Write your code below this line.
int n=binary.size();
cout <<binary[0];
for (int i =1;i<n;i++)
{
if (binary[i]==0 & binary[i-1]==0) cout<< "0";
else if(binary[i]==1 & binary[i-1]==1) cout<< "0";
else cout<<"1";
}
return "-1";
}

/* Question : Function to convert a gray code string to


binary string as explained in the problem description. */
string graytoBinary(string gray) {
// Write your code below this line.
int n=gray.size();
cout<<gray[0];
for (int i=1;i<n;i++)
{
while (gray[i]!='\0')
{
gray[i]=gray[i]^ gray[i-1];
i++;
}
cout <<gray[i];
}

return "-1";
}
int main(){
string binary;
string gray;
cin>> binary;
cin>>gray;
binarytoGray(binary);
graytoBinary(gray);
}

You might also like