OneCompiler

vrc.c

135

// VRC(Vertical Redundancy Check)
// Only can detect SINGLE BIT ERROR

#include<stdio.h>
#include<string.h>

int main()
{

	char msg[50];
	printf("Enter message bits : ");
	scanf("%s",msg);
	
	int msglen = strlen(msg);
	
	int parity;
	printf("Enter the type of parity ");
	printf("Enter 2 for Even Parity\nEnter 3 for Odd Parity : ");
	scanf("%d",&parity);
	
	int count_ones = 0;
	
	for(int i = 0;i < msglen;i++)
	{
		if(msg[i] == '1')
		{
			count_ones++;
		}
	}
	
	if(parity == 2)
	{
		if(count_ones % 2 == 0)
		{
			msg[msglen] = '0';
		}
		else{
			msg[msglen] = '1';
		}
	}
	else{
		if(count_ones % 2 == 0)
		{
			msg[msglen] = '1';
		}
		else{
			msg[msglen] = '0';
		}
	}
	
	msg[msglen+1] = '\0';
	printf("Message to be transmitted : %s\n",msg);
	
	printf("Enter message received on RECEIVER SIDE : ");
	scanf("%s",msg);
	
	msglen = strlen(msg);
	
	for(int i = 0;i < msglen;i++)
	{
		if(msg[i] == '1')
		{
			count_ones++;
		}
	}
	
	if(parity == 2)
	{
		if(count_ones % 2 == 0)
		{
			printf("Successful!!!,No bit changed");
		}
		else{
			printf("Error,bit changed");
		}
	}
	else{
		if(count_ones % 2 == 0)
		{
			printf("Error,bit changed\n");
		}
		else{
			printf("Successful!!!,No bit changed\n");
		}
	}

return 0;
}