The codeword is of 5 bits with the condition that there should be at most 1 leading and at most 1 trailing zero. So we can either have
- no Zero in the MSB and LSB
- 1 Zero in either MSB or LSB or both
The first case will look like: 1 _ _ _ 1 ( Since we cannot have 0 in MSB and LSB).
The second case will look like: 0 _ _ _ 1 (one 0 in MSB), 1 _ _ _ 0 (One 0 in LSB), 0 _ _ _ 0 (Both MSB and LSB is 0).
Let me the number the position of bits as 0 through 4 from LSB to MSB.
In the first case, the remaining 3 positions in 1 _ _ _ 1 can be filled in 2 ways each, i.e., each position can be either 0 or 1. So total no of such codes = 2x2x2=8 codewords.
In the second case:
in 0 _ _ _ 1, 3rd position bit can be filled with 1 only and not 0 because if the 3rd bit gets 0 then we will end up having two leading 0s in 4th and 3rd position. However, 2nd and 1st bit can be filled with 1 or 0 each. So for 3rd position, we have 1 choice, for 2nd and 1st position we have 2 choices each. So total no. of such codes=1x2x2=4 codewords.
Similarly, in 1 _ _ _ 0, the 1st position can be filled with 1 only because if the 1st position gets 0 then we will end up having two trailing zeroes in 1st and 0th position.Now the 2nd and 3rd position can be filled in two ways each, i,e, with 0 or 1. So total no of such codes=2x2x1=4 codewords.
On similar grounds, in the case of 0 _ _ _ 0, the 1st and 3rd position bits can be only 1 and not 0 to avoid having two leading and trailing zeroes. But the 2nd position can be 0 or 1, so 2 choices. Total no of such codes=1x2x1=2 codewords.
Total possible codewords=8+4+4+2=18 codewords => Option C