Parity

Description:

Ram and Sita playing the parity game. Two types of parity are there. One is odd parity and next is even parity. Ram will say the parity type, number of bits present in the message. Sita will find out the parity value and then she append the parity bit with the message bit. Sita will calculate the parity bit by counting the total number of one present int the message told by Ram. If she need to convert to even parity, the total count of one is even then the parity value is 0 otherwise 1 vice verse for odd parity conversion. calculate the parity value for message need to send. First line the type of parity.Second line is the number of bits present in the message.Followed by the Message bits. Test Case 1 Input (stdin) even 5 1 1 1 0 1 Expected Output 111010 Test Case 2 Input (stdin) odd 5 1 1 1 0 1 Expected Output 111011

Program :

#include<stdio.h>

#include<string.h>

int main()

{

    char parity_value[7],parity_check[7];

    int message_index,count=0;

    int message[100];

    int i;

    scanf ("%[^\n]s",parity_value);

    scanf("%d",&message_index);


    for (i=0;i<message_index;i++)

    {

        scanf("%d",&message[i]);

    }

     for(i=0;i< message_index;i++)

    {

        if(message[i]==1)

            count++;

    }

        if(count%2==0)

        {

        strcpy(parity_check,"even");

        }

    else

    {

       strcpy(parity_check,"odd");

    }


    if(strcmp(parity_check,parity_value)==0)

    {

        message[message_index]=0;

    }


    else

    {

       message[message_index]=1;

    }


    for(i=0;i<=message_index;i++)

    {

        printf("%d",message[i]);

    }

    return 0;

}