Vous êtes sur la page 1sur 30

# Computer Network Lab Manual

## Week 1a : Implement the data link layer farming methods such as

a. Bit stuffing.
Problem Statement1:
1. Implement the data link layer farming method Bit stuffing to add 0 to each set of 4 ones in given string
011110
Problem Statement2:
2. Implement the data link layer farming method Bit stuffing to add !"#hite space\$ to each set of % ones in
given string 0111110
Problem Statement3:
3. Implement the data link layer farming method Bit stuffing to add & to each set of ' ones in given string
01111110
Problem Statement4:
4. Implement the data link layer farming method Bit stuffing to add ( to each set of ' ones in given string
01111110
Problem Statement4:
5. Implement the data link layer farming method Bit stuffing to add ) to each set of * ones in given string
011111110
1: +o the given ,its add the string 011110 to ,oth the starting and the end of the string
a. +o add 0 to each set of 4 ones i.e. after every five consecutive 1-s appear a
.ero
/. +o the given ,its add the string 011110 to ,oth the starting and the end of the string
a. +o add 0 to each set of 4 ones i.e. after every five consecutive 1-s appear a
.ero
0include 1stdio.h1
0include 1conio.h1
int main"\$
2
int i304count305
char a610075
clrscr"\$5
printf"1enter the ,its : 1\$5
scanf"18s14a\$5
printf"19n:fter ,it stuffing 9n1\$5
printf"10111101\$5
for"i305a6i75 i;;\$
2
if"a6i733<1<\$
count;;5
else
count305
printf"18c14a6i7\$5
if"count334\$
2
printf"101\$5
count305
=
=
printf"101111101\$5
getch"\$5
return 15
=
Output:
>. +o the given ,its add the string 01111110 to ,oth the starting and the end of the string
a. +o add 0 to each set of % ones i.e. after every five consecutive 1-s appear a
.ero
Program:
0include 1stdio.h1
0include 1conio.h1
int main"\$
2
int i304count305
char a610075
printf"1enter the ,its : 1\$5
scanf"18s14a\$5

printf"19n:fter ,it stuffing 9n1\$5
printf"101111101\$5

for"i305a6i75 i;;\$
2
if"a6i733<1<\$
count;;5
else
count305
printf"18c14a6i7\$5
if"count33%\$
2
printf"101\$5
count305
=
=
printf"1011111101\$5
getch"\$5
return 15
=
4. +o the given ,its add the string 011111110 to ,oth the starting and the end of the
string
a. +o add 0 to each set of ' ones i.e. after every five consecutive 1-s appear a
.ero
Program:
0include 1stdio.h1
0include 1conio.h1
int main"\$
2
int i304count305
char a610075
printf"1enter the ,its : 1\$5
scanf"18s14a\$5

printf"19n:fter ,it stuffing 9n1\$5
printf"1011111101\$5

for"i305a6i75 i;;\$
2
if"a6i733<1<\$
count;;5
else
count305
printf"18c14a6i7\$5
if"count33'\$
2
printf"101\$5
count305
=
=
printf"10111111101\$5
getch"\$5
return 15
Output:
%. +o the given ,its add the string 0111111110 to ,oth the starting and the end of the
string
a. +o add 0 to each set of * ones i.e. after every five consecutive 1-s appear a
.ero
Program:
0include 1stdio.h1
0include 1conio.h1
int main"\$
2
int i304count305
char a610075
printf"1enter the ,its : 1\$5
scanf"18s14a\$5

printf"19n:fter ,it stuffing 9n1\$5
printf"10111111101\$5

for"i305a6i75 i;;\$
2
if"a6i733<1<\$
count;;5
else
count305
printf"18c14a6i7\$5
if"count33*\$
2
printf"101\$5
count305
=
=
printf"101111111101\$5
getch"\$5
return 15
Output:
'. +o the given ,its add the string 01111111110 to ,oth the starting and the end of the
string
a. +o add 0 to each set of ? ones i.e. after every five consecutive 1-s appear a
.ero
0include 1stdio.h1
0include 1conio.h1
int main"\$
2
int i304count305
char a610075
clrscr"\$5
printf"1enter the ,its : 1\$5
scanf"18s14a\$5

printf"19n:fter ,it stuffing 9n1\$5
printf"101111111101\$5
for"i305a6i75 i;;\$
2
if"a6i733<1<\$
count;;5
else
count305
printf"18c14a6i7\$5
if"count33?\$
2
printf"101\$5
count305
=
=
printf"1011111111101\$5
getch"\$5
return 15
=
*. +o the given ,its add the string 011111111110 to ,oth the starting and the end of the
string.
+o add 0 to each set of @ ones i.e. after every five consecutive 1-s appear a .ero
Arogram:
0include 1stdio.h1
0include 1conio.h1
int main"\$
2
int i304count305
char a610075
clrscr"\$5
printf"1enter the ,its : 1\$5
scanf"18s14a\$5

printf"19n:fter ,it stuffing 9n1\$5
printf"1011111111101\$5
for"i305a6i75 i;;\$
2
if"a6i733<1<\$
count;;5
else
count305
printf"18c14a6i7\$5
if"count33?\$
2
printf"101\$5
count305
=
=
printf"10111111111101\$5
getch"\$5
return 15
=
Output:
?. +o the given ,its add the string 011111111110 to ,oth the starting and the end of the
string.
+o add 0 to each set of 10 ones i.e. after every five consecutive 1-s appear a .ero
Arogram:
0include 1stdio.h1
0include 1conio.h1
int main"\$
2
int i304count305
char a610075
clrscr"\$5
printf"1enter the ,its : 1\$5
scanf"18s14a\$5

printf"19n:fter ,it stuffing 9n1\$5
printf"1011111111101\$5
for"i305a6i75 i;;\$
2
if"a6i733<1<\$
count;;5
else
count305
printf"18c14a6i7\$5
if"count3310\$
2
printf"101\$5
count305
=
=
printf"101111111111101\$5
getch"\$5
return 15
=
Butput:
9. C program to convert a ec!mal number to !t" e#u!valent \$!t" %ormat.
Program
0include Cstdio.hD
0include Cconio.hD
void main"\$
2
unsigned long dec5
int a6/%74c304i5
clrscr"\$5
printf"19nEF+EG : HEIIJ:K FLJBEG: 1\$5
scanf"18lu14&dec\$5
printf"19n8lu IF BI+M NBGJ:+: 14dec\$5
#hile"decD0\$
2
a6c73dec8/5
dec3decO/5
c;;5
=
for"i3cP15iD305iPP\$
printf"18d14a6i7\$5
getch"\$5
=
Output
Enter a decimal num,er 4
4 in ,it format : 100
1&. Searc' a ke( number w!t'!n n g!ven number" u"!ng po!nter"
Program
0include Cstdio.hD
0include Cconio.hD
0include Calloc.hD
void main"\$
2
int n4Qp4i4num4flag305
clrscr"\$5
printf"19nRBW J:FS FLJBEG: 1\$5
scanf"18d14&n\$5
p3"int Q\$ malloc"nQ/\$5
if"p33FLKK\$
2
printf"19nJEJBGS :KKBI:+IBF LFMLIIEMMNLK1\$5
eTit"\$5
=
for"i305iCn5i;;\$
2
printf"19nEF+EG FLJBEG 8d: 14i;1\$5
scanf"18d14p;i\$5
=
printf"19nEF+EG : FLJBEG +B ME:GIR: 1\$5
scanf"18d14&num\$5
for"i305iCn5i;;\$
2
if"num33Q"p;i\$\$
flag315
=
if"flag331\$
printf"19n+RE FLJBEG 8d IM NBLFH14num\$5
else
printf"19n+RE FLJBEG 8d HBEM FB+ EUIM+14num\$5
getch"\$5
=
Butput:
11. C program %or 'e)aec!mal to \$!t" conver"!on
Program
0includeCstdio.hD
0define J:U 1000
int main"\$2
char ,inaryFum,er6J:U74heTaHecimal6J:U75
long int i305
scanf"18s14heTaHecimal\$5
printf"19nEVuivalent ,its value: 1\$5
#hile"heTaHecimal6i7\$2
s#itch"heTaHecimal6i7\$2
case <0<: printf"100001\$5 ,reak5
case <1<: printf"100011\$5 ,reak5
case </<: printf"100101\$5 ,reak5
case <><: printf"100111\$5 ,reak5
case <4<: printf"101001\$5 ,reak5
case <%<: printf"101011\$5 ,reak5
case <'<: printf"101101\$5 ,reak5
case <*<: printf"101111\$5 ,reak5
case <?<: printf"110001\$5 ,reak5
case <@<: printf"110011\$5 ,reak5
case <:<: printf"110101\$5 ,reak5
case <B<: printf"110111\$5 ,reak5
case <I<: printf"111001\$5 ,reak5
case <H<: printf"111011\$5 ,reak5
case <E<: printf"111101\$5 ,reak5
case <N<: printf"111111\$5 ,reak5
case <a<: printf"110101\$5 ,reak5
case <,<: printf"110111\$5 ,reak5
case <c<: printf"111001\$5 ,reak5
case <d<: printf"111011\$5 ,reak5
case <e<: printf"111101\$5 ,reak5
case <f<: printf"111111\$5 ,reak5
default: printf"19nInvalid heTadecimal digit 8c 14heTaHecimal6i7\$5 return 05
=
i;;5
=
return 05
getch"\$5
=
Output:
Week I,: Implement the data link layer framing methods such as character stuffing.
C program to insert substring into a string at beging
0include Cstdio.hD
0include Cstring.hD
0include Cstdli,.hD

void insertWsu,string"charQ4 charQ4 int\$5
charQ su,string"charQ4 int4 int\$5

int main"\$
2
char teTt610074 su,string610075
int position5

printf"1Enter some teTt9n1\$5
gets"teTt\$5

printf"1Enter the string to insert9n1\$5
gets"su,string\$5

printf"1Enter the position to insert9n1\$5
scanf"18d14 &position\$5

insertWsu,string"teTt4 su,string4 position\$5

printf"18s9n14teTt\$5

return 05
=

void insertWsu,string"char Qa4 char Q,4 int position\$
2
char Qf4 Qe5
int length5

length 3 strlen"a\$5

f 3 su,string"a4 14 position P 1 \$5
e 3 su,string"a4 position4 lengthPposition;1\$5

strcpy"a4 11\$5
strcat"a4 f\$5
free"f\$5
strcat"a4 ,\$5
strcat"a4 e\$5
free"e\$5
=

char Qsu,string"char Qstring4 int position4 int length\$
2
char Qpointer5
int c5

pointer 3 malloc"length;1\$5

if" pointer 33 FLKK \$
eTit"EUI+WN:IKLGE\$5

for" c 3 0 5 c C length 5 c;; \$
Q"pointer;c\$ 3 Q""string;positionP1\$;c\$5

Q"pointer;c\$ 3 <90<5

return pointer5
=
Output:
C program to insert substring into a string at end
0include Cstdio.hD
0include Cstring.hD
0include Cstdli,.hD

void insertWsu,string"charQ4 charQ4 int\$5
charQ su,string"charQ4 int4 int\$5

int main"\$
2
char teTt610074 su,string610075
int position5

printf"1Enter some teTt9n1\$5
gets"teTt\$5

printf"1Enter the string to insert9n1\$5
gets"su,string\$5

printf"1Enter the position to insert9n1\$5
scanf"18d14 &position\$5

insertWsu,string"teTt4 su,string4 position\$5

printf"18s9n14teTt\$5

return 05
=

void insertWsu,string"char Qa4 char Q,4 int position\$
2
char Qf4 Qe5
int length5

length 3 strlen"a\$5

f 3 su,string"a4 14 position P 1 \$5
e 3 su,string"a4 position4 lengthPposition;1\$5

strcpy"a4 11\$5
strcat"a4 f\$5
free"f\$5
strcat"a4 ,\$5
strcat"a4 e\$5
free"e\$5
=

char Qsu,string"char Qstring4 int position4 int length\$
2
char Qpointer5
int c5

pointer 3 malloc"length;1\$5

if" pointer 33 FLKK \$
eTit"EUI+WN:IKLGE\$5

for" c 3 0 5 c C length 5 c;; \$
Q"pointer;c\$ 3 Q""string;positionP1\$;c\$5

Q"pointer;c\$ 3 <90<5

return pointer5
=
Output:
C program to insert substring into a string at Middle
0include Cstdio.hD
0include Cstring.hD
0include Cstdli,.hD
void insertWsu,string"charQ4 charQ4 int\$5
charQ su,string"charQ4 int4 int\$5
int main"\$
2
char teTt610074 su,string610075
int position5
printf"1Enter some teTt9n1\$5
gets"teTt\$5
printf"1Enter the string to insert9n1\$5
gets"su,string\$5
printf"1Enter the position to insert9n1\$5
scanf"18d14 &position\$5
insertWsu,string"teTt4 su,string4 position\$5
printf"18s9n14teTt\$5
return 05
=
void insertWsu,string"char Qa4 char Q,4 int position\$
2
char Qf4 Qe5
int length5
length 3 strlen"a\$5
f 3 su,string"a4 14 position P 1 \$5
e 3 su,string"a4 position4 lengthPposition;1\$5
strcpy"a4 11\$5
strcat"a4 f\$5
free"f\$5
strcat"a4 ,\$5
strcat"a4 e\$5
free"e\$5
=
char Qsu,string"char Qstring4 int position4 int length\$
2
char Qpointer5
int c5
pointer 3 malloc"length;1\$5
if" pointer 33 FLKK \$
eTit"EUI+WN:IKLGE\$5

for" c 3 0 5 c C length 5 c;; \$
Q"pointer;c\$ 3 Q""string;positionP1\$;c\$5
Q"pointer;c\$ 3 <90<5
return pointer5
=
Output:
C program to %!n out t'e lengt' o% a g!ven "tr!ng w!t'out u"!ng t'e l!brar( %unct!on
"trlen*+.
0include Cstdio.hD
0include Cconio.hD
void main"\$
2
char str6%075
int len5
clrscr"\$5
printf"19nEF+EG : M+GIFX: 1\$5
gets"str\$5
for"len305str6len7Y3FLKK5len;;\$5
printf"19n+RE KEFX+R BN +RE M+GIFX IM 8d14len\$5
getch"\$5
=
Output:
EF+EG : M+GIFX : :rchana
n+RE KEFX+R BN +RE M+GIFX IM : *
,o pr!nt t'e rever"e o% a g!ven "tr!ng
0include Cstdio.hD
0include Cconio.hD
void main"\$
2
char ch610075
int i4len5
clrscr"\$5
printf"19nEF+EG : M+GIFX: 1\$5
gets"ch\$5
len3strlen"ch\$5
printf"19n+RE M+GIFX IF +RE GEZEGME BGHEG: 1\$5
for"i3lenP15iD305iPP\$
printf"18c14ch6i7\$5
getch"\$5
=
Butput:
EF+EG : M+GIFX ::rchana
+RE M+GIFX IF +RE GEZEGME BGHEG: anahcra
C program to compare two strings using strcmp
Arogram:
include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];
clrscr();

print(!"nter the irst string\n!);
gets(a);

print(!"nter the second string\n!);
gets(b);

i( strcmp(a,b) ## 0 )
print(!"ntered strings are e\$ual.\n!);
else
print(!"ntered strings are not e\$ual.\n!);

return 0;
=
Butput:
c program to copy a string using pointers
#include<stdio.h>

%oid cop&'string(char(, char();

main()
{
char source[100], target[100];

print(!"nter source string\n!);
gets(source);

cop&'string(target, source);

print(!)arget string is \"*s\"\n!, target);

return 0;
+

%oid cop&'string(char (target, char (source)
{
,hile((source)
{
(target # (source;
source--;
target--;
+
(target # .\0.;
+
Output:
String concatenation without strcat
#include <stdio.h>

%oid concatenate'string(char(, char();

int main()
{

print(!"nter source string\n!);
gets(original);

print(!"nter string to concatenate\n!);

print(!/tring ater concatenation is \"*s\"\n!, original);

return 0;
+

{
,hile((original)
original--;

{
original--;
+
(original # .\0.;
+
Butput:
Change string to lower case without strlwr
#include <stdio.h>

%oid lo,er'string(char();

int main()
{
char string[100];

print(!"nter a string to con%ert it into lo,er case\n!);
gets(string);

lo,er'string(string);

print(!"ntered string in lo,er case is \"*s\"\n!, string);

return 0;
+

%oid lo,er'string(char (string)
{
,hile((string)
{
i ( (string ># .0. 11 (string <# .2. )
{
(string # (string - 34;
+
string--;
+
+
Butput:
Change string to upper case without strupr
#include <stdio.h>

%oid upper'string(char();

int main()
{
char string[100];

print(!"nter a string to con%ert it into upper case\n!);
gets(string);

upper'string(string);

print(!"ntered string in upper case is \"*s\"\n!, string);

return 0;
+

%oid upper'string(char (string)
{
,hile((string)
{
i ( (string ># .a. 11 (string <# .5. )
{
(string # (string 6 34;
+
string--;
+
+
Butput