can someone help me with a program that reads in a sequence of binary digits that in a container. The input terminates on any input that is not a 0 or 1.Also adding bit-stuffing algorithm that occurs after four consecutive bits of the same value.
As an output, the original data stream, the bit stuffed data and the number of stuffed bits that have been added are displayed.
The de-stuffing code is also needed to process the stuffed data to recreate the original data and verify that the original data is recovered correctly.
// read bits '0'/'1' into a string; terminate on any other char
std::string data_stream ;
char bit ;
while( std::cin >> bit && ( bit == '0' || bit == '1' ) ) data_stream += bit ;
// stuff a zero bit after four consecutive bits of the same value.
std::string stuffed_stream ;
int cnt = 0 ;
char bit_last_seen = 0 ;
int stuff_bits_added = 0 ;
for each bit in the data_stream
if( it is the same as the previous bit ) increment cnt
else // it is a different bit
bit_last_seen := this bit ;
cnt := 1 ; // restart count at 1
stuffed_stream += bit ; // add the bit
if( cnt == 4 ) // there are four consecutive bits of the same value
stuffed_stream += '0' ; // stuff with a zero bit
cnt := 0 ; // and reset cnt to zero
++stuff_bits_added ; // increment the count of stuff bits added
// print out the results
std::cout << " data stream: " << data_stream << '\n'
<< " stuffed stream: " << stuffed_stream << '\n'
<< "stuff bits added: " << stuff_bits_added << '\n' ;
// The de-stuffing code to process the stuffed data to recreate the original
// and verify that the original data is recovered correctly.