C++ program for bit-stuffing..

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.
i am sorry but i din get what wrong did i post?
i needed help for a program with bit stuffing, i did not mean the whole program but the logic or concept would be really helpful.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <string>

int main()
{
    // 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' ;
               
               
    // TODO:
    // The de-stuffing code to process the stuffed data to recreate the original 
    // and verify that the original data is recovered correctly.           
}
Topic archived. No new replies allowed.