Wednesday, June 5, 2013

CHECKSUM C Program



CHECKSUM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
        char a[10],b[10],c[10],d[10];
       int count=0,i,x,ch,ch1,temp,n,y;
       clrscr();
       printf("enter string lenght");
       scanf("%d",&n);
       printf("enter 1st string: ");
       scanf("%s",a);
       printf("enter 2nd string: ");
       scanf("%s",b);
       a[n+1]='\0';
       b[n+1]='\0';
            for(i=0;a[i],b[i]!=NULL;i++)
            {
                      temp=a[i]+b[i];
                       if(temp%2==0)
                        {
                                c[i]='0';
                         }
                       Else
{       
                                c[i]='1';
}
            }
            c[n]='\0';
            for(i=0;a[i],b[i]!=NULL;i++)
            {
                     if(c[i]=='1')
                      count++;
            }
            printf("\nnew string is :%s",c);
            for(i=0;c[i]!=NULL;i++)
            {
                     if(c[i]=='0')
                               c[i]='1';
                     else
                               c[i]='0';
            }
            printf("\nans is :%s",c);
            printf("\ndo you want to add error ?\n1:yes\n2:no");
            scanf("%d",&ch);
            switch(ch)
            {
                      case 1:
                      printf("\nIn which string you want to enter error ?\n 1:string1\n2:string2");
                      scanf("%d",&ch1);
              if(ch1==1)
                      {
                               printf("\nIn which bit you want to enter the error ?");
                               scanf("%d",&x);
                               if(a[x]=='0')
                                         a[x]='1';
                               else
                                         a[x]='0';
                               printf("\n%s",a);
                       }
                      if(ch1==2)
                      {
                               printf("\nIn which bit you want to enter error ?");
                               scanf("\n%d",&x);
                               if(b[x]=='0')
{
                                         b[x]='1';
}
                               Else
{
                                         b[x]='0';
}|
                               printf("\n%s",b);
                      }
                      break;
            case 2:
                      printf("\nno need to enter error");
                      printf("\n%s",c);
                      break;
}
        for(i=0;a[i]!=NULL;i++)
                           {
                             temp=a[i]+b[i]+c[i];
                                       if(temp%2==0)
{
                                                 d[i]='0';
}
                                        Else
{
                                                 d[i]='1';
}       
                            }
d[n]='\0';
                           for(i=0;d[i]!=NULL;i++)
{
            if(d[i]=='1')
             {
                     d[i]='0';
             }
            Else
{
                      d[i]='1';
}
}
printf("\nfinal string is :%s",d);
for(i=0;d[i]!=NULL;i++)
{
            if(d[i]=='1')
            count++;
}
if(count%2==0)
{
            printf("\nthere is no error");
 }
Else
{
            printf("\nerror is generated");
}
getch();
}

                    Output:-
enter string length 8
enter 1st string: 10101010
enter 2nd string: 10101010
new string is :00000000
ans is :11111111
do you want to add error ?
1:yes
2:no
1
In which string you want to enter error ?
 1:string1
2:string2
1
In which bit you want to enter the error ?2
10001010
final string is :00100000
error is generate

No comments:

Post a Comment