Coding Practice

Write a C/JAVA program to implement CRC

Implement CRC

The program will take a sequence of binary data bits, and a divider as input. And gives the resultant data with CRC as output. (Where, divider < binary data bits)

Sample Input:
Data: 10110
Divider: 1101

Sample Output:
Modified data: 10110000
Data Send: 10110101
CRC bit: 101

Sample Output
Enter data: 1001
Enter divisor: 1011

Input Data With Zero(s): 1001000

CRC Bit: 110

Modified Data: 1001110
Source Code
#include<stdio.h>
#include<stdlib.h>

void main()
{
    int i, j, k, l, numOfZero, dataLen;
    char divisor[100], data[100], remainder[100], divisorClone[100], dataClone[100], crc[100];

    printf("Enter data: ");
    scanf("%s", data);
    printf("Enter divisor: ");
    scanf("%s", divisor);

    for(i = 0; data[i] != '\0'; i++);
    dataLen = i;
    for(i = 0; divisor[i] != '\0'; i++);
    numOfZero = i;

    for(i = 0; i < numOfZero-1; i++)
    {
        data[dataLen+i] = '0';
    }
    printf("\nInput Data With Zero(s): %s\n", data);
    strcpy(dataClone, data);

    for(i = 0; i < dataLen; i++)
    {
        strcpy(divisorClone, divisor);
        if(data[i] < divisor[0])
        {
            for(l = 0; l < numOfZero; l++)
            {
                divisor[l] = '0';
            }
            divisor[l] = '\0';
        }
        for(j = i, k = 0; j < numOfZero+i; j++, k++)
        {
            if(data[j] == divisor[k])
            {
                remainder[j] = '0';
            }
            else
            {
                remainder[j] = '1';
            }
        }
        strcpy(divisor, divisorClone);
        remainder[j] = dataClone[j];
        remainder[j+1] = '\0';
        strcpy(data, remainder);
    }
    strcpy(data, remainder);

    for(i = dataLen, j = 0; i < dataLen+numOfZero; i++)
    {
        dataClone[i] = data[i];
        crc[j++] = data[i];
    }
    crc[j-1] = '\0';
    dataClone[i-1] = '\0';

    printf("\nCRC Bit: %s\n\nModified Data: %s\n\n", crc, dataClone);
}
Sample Output
Enter data: 10111011
Enter divisor: 1001

Input Data With Zero(s): 10111011000

CRC Bit: 110

Modified Data: 10111011110

No comments:

Post a Comment

Change Theme
X