Sie sind auf Seite 1von 34

1.

Write a C program to take the input from a file and


convert the contents of the file into binary and print the
output

Source Code :

#include<stdio.h>
#include<conio.h>

void binary(int x)
{
int a[20],j,k;
j=0;
while(x>0)
{
a[j]=x%2;
x=x/2;
j++;
}
for(k=j-1;k>=0;k--)
{
printf("%d",a[k]);
}
}

void main()
{
char s[20];
int i;
clrscr();
puts("\n enter a string:");
gets(s);
printf("\n binary format is:\n");
for(i=0; s[i] != '\0'; i++)
{
binary(s[i]);
}
getch();
}

Output :

2. Write a C program to take the input from a file and convert


the contents of the file into binary and if there exists five
consecutive 1’s then insert a zero after five 1’s and print the
output

Source Code :

#include<stdio.h>
#include<conio.h>

int b[100],l=0;

void binary(int x)
{
int a[20],j,k;
j=0;
while(x>0)
{
a[j]=x%2;
x=x/2;
j++;
}
for(k=j-1;k>=0;k--)
{
b[l]=a[k];
l++;
}
}

void main()
{
FILE*fp,*ft;
char ch,num;
int i,doop[100],g,count;
clrscr();
fp=fopen("bt.txt","r");
ft=fopen("dt.txt","w");
ch=fgetc(fp);
while(ch!=EOF)
{
binary(ch);
ch=fgetc(fp);
}
printf("\n b array is:\n");
for(i=0;i<l;i++)
{
printf("%d",b[i]);
}
g=0;
for(i=0;i<l;i++)
{
if(b[i]==0)
{
doop[g]=b[i];
count=0;
g++;
}
else
{
doop[g]=b[i];
count++;
if(count==5)
{
g++;
doop[g]=0;
count=0;
}
g++;
}
}
printf("\n array after bit stuffing:\n");
for(i=0;i<g;i++)
{
printf("%d",doop[i]);
}
for(i=0;i<g;i++)
{
fprintf(ft,"%d",doop[i]);
}
fclose(fp);
fclose(ft);
getch();
}

Output :
3. Write a C program to take input from the user and perform
byte stuffing on the data and print the output

Source Code :

#include<stdio.h>
#include<conio.h>

char s[20],r[20],t[20];

void binary(char x)
{
int z,doop[10],i,l;
if(x=='F')
printf("01111110 ");
else if(x=='E')
printf("11100000 ");
else
{
i=0;
while(x>0)
{
z=x%2;
doop[i]=z;
i++;
x=x/2;
}
for(l=i-1;l>=0;l--)
printf("%d",doop[l]);
printf(" ");
}
}

void main()
{
int i,j;
FILE *fp;
clrscr();
printf("\n enter data:");
gets(s);
j=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]=='E')
{
r[j]='E';
j++;
r[j]='E';
j++;
}
else if(s[i]=='F')
{
r[j]='E';
j++;
r[j]='F';
j++;
}
else
{
r[j]=s[i];
j++;
}
}
printf("\n without adding headers:\n");
for(i=0;r[i]!='\0';i++)
printf("%c",r[i]);
t[0]='F';
t[1]='H';
i=2;
for(j=0;r[j]!='\0';j++)
{
t[i]=r[j];
i++;
}
t[i]='T';
t[i+1]='F';
printf("\n with headers:\n");
for(i=0;t[i]!='\0';i++)
printf("%c",t[i]);
printf("\n binary are:\n");
for(i=0;t[i]!='\0';i++)
binary(t[i]);
getch();
}
Output :
4. Write a C program to verify whether the data received by the
receiver is correct or not using CRC

Source Code :

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

char a[20],b[20],c[20],a1[20];
int lena,lenb,i,j;

void shift(char p[])


{
int k;
for(k=1;k<lenb;k++)
c[k-1]=c[k];
c[lenb-1]=p[i];
i++;
}

void xor(char p,char q)


{
if(p==q)
c[j]='0';
else
c[j]='1';
}
void calculator(char p[],char b[],char c[])
{
int y;
lena=strlen(p);
for(i=0 ;i<lenb ;i++)
c[i]=p[i];
i=lenb;
while(i<=lena)
{
if(c[0]=='1')
{
for(j=0;j<lenb;j++)
{
xor(c[j],b[j]);
}
}
else
{
for(j=0;j<lenb;j++)
{
xor(c[j],'0');
}
}
if(c[0]=='0')
shift(p);
printf("\n");
for(y=0;y<lenb;y++)
printf("%c",c[y]);
}
}
void main()
{
int y,z;
clrscr();
puts("\n enter a word:");
gets(a);
puts("\n enter a divisor:");
gets(b);
lena=strlen(a);
lenb=strlen(b);
for(y=0;y<lena;y++)
a1[y]=a[y];
for(i=lena;i<(lena+lenb-1);i++)
a[i]='0';
calculator(a,b,c);
printf("\n remainder is: %s",c);
lena=strlen(a1);
for(y=lena,z=0;y<(lena+lenb-1);y++,z++)
a1[y]=c[z];
calculator(a1,b,c);
printf("\n remainder is : %s",c);
printf("\n");
for(y=0;y<lenb-1;y++)
{
if(c[y]!='0')
goto v;
}
printf("\n no error in transmitting");
goto x;
v:
printf("\n error in transmitting");
X:
getch();
}

Output :

5. Write a C program to implement hamming code

Source Code :

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

char data[20],modify[20];
int i,r,m;

void method(int x)
{
int j,y,c,k,count;
y=x+1;
j=x;
c=0;
count=0;
printf("\n y=%d:",y);
while(j<(m+r))
{
if(c<y)
{
if(modify[j]=='1')
count++;
c++;
j++;
}
else
{
for(k=0;k<y;k++)
j++;
c=0;
}
}
if(count%2==0)
modify[x]='0';
else
modify[x]='1';
}

void main()
{
int k,j;
clrscr();
puts("\n enter dataword:");
gets(data);
m=strlen(data);
r=1;
while((pow(2,r)<(m+r+1)))
{
r++;
}
for(i=0;i<(m+r);i++)
{
for(k=0;k<(m+r);k++)
{
if(i+1==pow(2,k))
{
modify[i]='a';
break;
}
}
}
j=m-1;
for(i=0;i<(m+r);i++)
{
if(modify[i]!='a')
{
modify[i]=data[j];
j--;
}
}
printf("\n modify is : %s",modify);
for(i=0;i<(m+r);i++)
{
if(modify[i]=='a')
method(i);
}
printf("\n modify is : %s",modify);
printf("\n no error in transmission");

getch();
}

Output :
6. Write a C program to implement 1 bit sliding window protocol

Source Code :

#include<stdio.h>
#include<conio.h>

void main()
{
int a[20],p,lp,i,c,tm=0;
clrscr();
printf("\n enter the no of packets:");
scanf("%d", &p);
printf("\n enter the lost packet:");
scanf("%d", &lp);
i=1;
c=0;
printf("\n senders side:\n");
while(i<=p)
{
printf("\n sender side : %d acknowledgement",i);
if(c<lp-1)
printf("\n receiver side: %d data \n",i);
sleep(1);
c++;
i++;
tm++;
if(c==lp)
{
c=0;
i--;
sleep(1);
}
}
printf("\n \n number of transmissions: %d",tm);
getch();
}

Output :
7.Write C program to implement djikstra algorithm for single
source shortest path in a subnet

Source Code :

#include<stdio.h>
#include<conio.h>

void main()
{
int arr[30][30],vt,i,j,sw;
int modify[10][10],min,minj,ignore[10],k=0,z,flag,source,des;
clrscr();
printf("\n enter the number of vertices:");
scanf("%d", &vt);
for(i=1;i<=vt;i++)
{
for(j=1;j<=vt;j++)
{
printf("\n enter a[%d][%d]:",i,j);
scanf("%d", &arr[i][j]);
}
printf("\n");
}
printf("\n enter the source vertex:");
scanf("%d", &sw);
for(i=1;i<=vt;i++)
{
modify[1][i]=arr[sw][i];
}
for(i=1;i<=vt;i++)
{
printf("%d ",modify[1][i]);
}
for(i=1;i<=vt;i++)
{
min=1000;
for(j=1;j<=vt;j++)
{
flag=0;
for(z=0;z<k;z++)
{
if(ignore[z]==j || modify[i][j]==99)
{
flag=1;
break;
}
}
if(flag == 0)
{
if(min > modify[i][j] && modify[i][j]!=0)
{
min=modify[i][j];
minj=j;
ignore[k]=j;
k++;
}
}
}
for(j=1;j<=vt;j++)
{
if(min+arr[minj][j] < modify[i][j])
modify[i+1][j] = min+arr[minj][j];
else
modify[i+1][j] = modify[i][j];
}
}
printf("\n modify is:\n");
for(i=1;i<=vt;i++)
{
for(j=1;j<=vt;j++)
{
printf("%d ",modify[i][j]);
}
printf("\n");
}
printf("\n enter source and destination:");
scanf("%d%d",&source,&des);
if(modify[vt][des] == 99)
printf("\n no path found");
else
printf("\n shortest path from %d and %d is
%d",source,des,modify[vt][des]);
getch();
}
Output :

8.Write a C program to implement distance vector routing


algorithm

Source Code :

#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],b[10][10],v,i,j,k,via[10][10];
clrscr();
printf("\n enter the number of vertices");
scanf("%d",&v);
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
{
via[i][j]=-1;
}
}
printf("\n enter the direct values else 99:");
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
{
printf("\n a[%d][%d]=",i,j);
scanf("%d", &b[i][j]);
a[i][j]=b[i][j];
}
}
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
{
for(k=0;k<v;k++)
{
if(k>=i)
{
if(a[i][j] > (b[i][k]+b[k][j]))
{
a[i][j] = b[i][k]+b[k][j];
via[i][j] = k;
}
}
else
{
if(a[i][j] > (b[i][k]+a[k][j]))
{
a[i][j]=b[i][k]+a[k][j];
via[i][j]=k;
}
}
}
}
}
for(i=0;i<v;i++)
{
for(j=0;j<v;j++)
{
if(via[i][j] == -1)
printf("\n distance between %d and %d is %d
via %d",i,j,a[i][j],j);
else
printf("\n distance between %d and %d is %d
via %d",i,j,a[i][j],via[i][j]);
}
printf("\n");
sleep(1);
}
getch();
}

Output :
9. Write a C program to implement sink tree of broadcasting

Source Code :

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define INFINITY 9999
#define MAX 20

int g[MAX][MAX],st[MAX][MAX],n,i,j;
int cost[MAX][MAX],u,v,mindist,dist[MAX];
int from[MAX],visited[MAX],edges,mincost;
int prims();

int main()
{
int i,j,tot_cost;
printf("Enter total number of nodes : ");
scanf("%d", &n);
printf("Enter adjacency matrix :\n\n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
printf("g[%d][%d] : ",i,j);
scanf("%d",&g[i][j]);
}
}
tot_cost = prims();
printf("The spanning matrix of given matrix is :\n\n");
printf("\tA\tB\tC\tD\n");
for(i=0; i<n; i++)
{
printf("%c\t", i+'A');
for(j=0; j<n; j++)
{
printf("%d\t",st[i][j]);
}
printf("\n");
}
printf("The total cost is %d",tot_cost);
return 0;
}

int prims()
{
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
if(g[i][j] == 0)
cost[i][j] = INFINITY;
else
{
cost[i][j] = g[i][j];
st[i][j] = 0;
}
}
}
dist[0] = 0;
visited[0] = 1;
for(i=1; i<n; i++)
{
dist[i] = cost[0][i];
from[i] = 0;
visited[i] = 0;
}
mincost = 0;
edges = n-1;
while(edges > 0)
{
mindist = INFINITY;
for(i=1; i<n; i++)
{
if(visited[i] == 0 && dist[i] < mindist)
{
v = i;
mindist = dist[i];
u = from[v];
}
}
st[u][v] = dist[v];
st[v][u] = dist[v];
edges--;
visited[v] = 1;
for(i=1; i<n; i++)
{
if(visited[i] == 0 && cost[i][v] < dist[i])
{
dist[i] = cost[i][v];
from[i] = v;
}
}
mincost += cost[u][v];
}
return mincost;
}
Output :
10.Write a C program to implement Go-Back N protocol

Source Code :

#include<stdio.h>
#include<conio.h>

int arr[100];

void main()
{
int i,np,lp,ws,count,j,tm=0;
clrscr();
printf("\n enter the number of packets:");
scanf("%d", &np);
printf("\n enter the lost packet:");
scanf("%d", &lp);
printf("\n enter the window size:");
scanf("%d", &ws);
for(i=1; i <= np;i++)
{
arr[i]=i;
}
i=1;
count=1;
while(i<=np)
{
printf("\n sender ack-->%d",i);
printf("\n receiver data-->");
sleep(1);
for(j=0;j<ws;j++)
{
if(arr[i+j]!=0)
printf("%3d ",arr[i+j]);
}
tm++;
printf("\n");
count++;
i++;
if(count==lp)
{
count=1;
printf("\n");
sleep(1);
for(j=0;j<ws;j++)
{
if(arr[i+j]!=0)
printf("%3d ",arr[i+j]);
}
printf("--> lost \n");
tm++;
}
printf("\n");
}
printf("\n number of transmissions:%d",tm);
getch();
}
Output :
11.Write a C program to encrypt the input text and decrypt at
the receiver side

Source Code :

#include<stdio.h>
#include<conio.h>

void main()
{
char input[100],enc[100],dec[100];
int k,i,x,temp;
clrscr();
printf("\n enter the string:");
gets(input);
printf("\n enter the key:");
scanf("%d",&k);
for(i=0;input[i];i++)
{
if(input[i]>='A' && input[i]<='Z')
{
x=input[i]-'A';
temp=(x+k)%26;
enc[i]= temp+'A';
}
else if(input[i]>='a' && input[i]<='z')
{
x=input[i]-'a';
temp=(x+k)%26;
enc[i]= temp+'a';
}
}
enc[i]='\0';
printf("\n encrypted is: %s",enc);
for(i=0;enc[i];i++)
{
if(enc[i]>='A' && enc[i]<='Z')
{
x=enc[i]-'A';
temp=((x-k)+26)%26;
dec[i]= temp+'A';
}
else if(enc[i]>='a' && enc[i]<='z')
{
x=enc[i]-'a';
temp=((x-k)+26)%26;
dec[i]= temp+'a';
}
}
dec[i]='\0';
printf("\n decrypted is: %s",dec);
getch();
}

Output :

Das könnte Ihnen auch gefallen