Search code examples
c++stringif-statementdecoder

Change a bunch of numbers into corresponding alphabets


There are five types of bit encoding: 11, 10, 01, 001, 000.The corresponding message for each bit encoding is

11 -> A

10 -> B

01 -> C

001 -> D

000 -> E

The compile result will look like this:

Enter a sequence of bits: 01100001100110
CBEADB

I've wrote this but it can't work.

#include<iostream>
#include<string>
using namespace std;

int main()
{
    int A, B, C, D, E ;
    string code;
    cout << "Enter a sequence of bits: ";
    cin >> code;
    int i = 0;
    while (i < code.length())
    {
        if( code[i]== 1 && code[i + 1]== 1 )
        {
            cout << 'A';
            i += 2;
        }
        else if ( code[i]== 1 && code[i + 1]== 0)
        {
            cout << 'B';
            i += 2;
        }
        else if ( code[i]== 0 && code[i + 1]== 1)
        {
            cout << 'C';
            i += 2;
        }
        else if ( code[i]== 0 && code[i + 1]== 0 && code[ i + 2]== 1)
        {
            cout << 'D';
            i += 3;
        }
        else if ( code[i]== 0 && code[i + 1]== 0 && code[ i + 2]== 0)
        {
            cout << 'E';
            i += 3;
        }
    }
    cout << endl;
    return 0;
}

Solution

  • Your input is a String not numbers, therefore you have to check for characters. Instead of using something like == 0, you should be using something like == '0'. Also, you may want to make sure that the input has the right format and you don't run into an infinite loop (by running the while forever without updating i) or buffer overflow (aka. index out of range, by checking code[i] with i >= code.length()). You can do something like:

    #include<iostream>
    #include<string>
    using namespace std;
    
    int main()
    {
        int A, B, C, D, E ;
        string code;
        cout << "Enter a sequence of bits: ";
        cin >> code;
        int i = 0;
        while (i+1 < code.length())
        {
            if( code[i]== '1' && code[i + 1]== '1' )
            {
                cout << 'A';
                i += 2;
            }
            else if ( code[i]== '1' && code[i + 1]== '0')
            {
                cout << 'B';
                i += 2;
            }
            else if ( code[i]== '0' && code[i + 1]== '1')
            {
                cout << 'C';
                i += 2;
            }
            else if (i+2 < code.length()) 
            {
                if (code[i]== '0' && code[i + 1]== '0' && code[ i + 2]== '1')
                {
                    cout << 'D';
                    i += 3;
                    }
                    else if ( code[i]== '0' && code[i + 1]== '0' && code[ i + 2]== '0')
                    {
                        cout << 'E';
                        i += 3;
                    } 
                    else 
                    {
                        return 1;
                    }
            }
            else 
            {
                return 1;
            }
        }
        if (i < code.length()) {
            return 1;
        }
        cout << endl;
        return 0;
    }
    

    You can try it out here.