C Lab Manual

Download as pdf or txt
Download as pdf or txt
You are on page 1of 43

Laboratory Manual

For
Programming Lab (IT291)

B.Tech. (IT)
SEM II

January, 2014

DEPARTMENT OF INFORMATION TECHNOLOGY


JIS COLLEGE ENGINEERING
(AN AUTONOMOUS INSTITUTE)

LAB COURSE DESCRIPTION


1. Course code:

IT291

2. Course Title:

Programming Lab

3. Core

Elective

4. Pre-requisites (if any):

C Language

5. Year/Semester in which offered:

I year II Semester

6. No. of hours per week:

7. Course objective:
8. List of programs:

separate sheet has been attached

9. Evaluation procedure

INDEX

DESCRIPTION EVALUATION

TOTAL MARKS

Marks allotted for day-to-day lab work

15 M

Marks allotted for attendance

5M

Marks awarded for viva voce/quiz

5M

Marks awarded for Lab Exam

15 M

Final sessional marks(A+B+C+D)

40 M

University Examination Marks

60 M

Experiment process:
Required Software/ Software Tool:
- Windows Operating System and/ or Linux Operating System
- Turbo C/C++ IDE.
Common Procedure:
Step 1: For the given problem statement design
Flowchart/Algorithm/Logic
Step 2: Define variables and functions which will show the flow of program
I: Create a folder in either E or F drive with your Id Number or Name Followed by
RollNo.
II: Start the TC (Turbo C) from Desktop Icon or Go To Directory D:/TC/BIN/ and
run tc.exe. An Editor window will be opened.
III: Click on File Menu --> New. New (.c) file will be created. Write C code in the file
with .c extension. Again Click on File - Save a dialog box is going open write the path to
your directory e.g. E:\Student_Roll\FileName.C and Press OK. Now your C program is
going to save at your directory.
IV: Go To Option-->Directories Check that Include Directory is Set As
D:\TC\Include and Library Directory is Set To D:\TC\LIB.
Step 3: If the OS is Windows than compile and run .c file using turbo C.
Compile code using gcc compiler for Linux, which will create a.out executable file.
Step 4: Test the program using sample input and write down output.

Exercise 1:
Write a Program in C to find the sum of individuals Digits of a positive Integer.

Solution:

Algorithm
1)
2)
3)
4)

Input a number n.
Check n is +ve, if not goto step 6.
S=0,m=n.
Repeat the following until n=0.
i. r=n%10.
ii. s+=s+r.
iii. n=n/10.
5) print s, m.
6) stop.

Flowchart
Start

Input n

Yes

No

If
n<0

S=0.m=n
Yes

Repeat until n=0


No

r=n%10
S=s+r
n=n/10
Print not possible
print m, s

Stop

Exercise 2:
Write a program a Fibonacci sequence is defined as follows.
The first and second terms in the sequence are 0 and 1. Subsequent terms are found by
adding the precedes two preceding two terms in the sequence, Write a C Program to
Generate the first n terms of the sequence.
Solution:

Algorithm
1.
2.
3.
4.
5.

Enter the no of Fibonacci, n to be generated.


f1=0
f2=1
print f1,f2
repeat the following for n-2 times.
a. f=f1+f2
b. print f
c. f1=f2
d. f2=f
6. stop.

Flowchart

Start

Input n

f1=0, f2=1
Print f1, f2
Repeat the following for
n-2 times

f=f1+f2
Print f
f1=f2
f2=f

Stop

Exercise 3:
Write a C program to generate all the prime numbers between 1 and n,
where n is a value supplied by the user.

Solution:

Algorithm:
1. Enter a no n
2. for i=1 to n do
(a) k=prime(i).
(b) if(k==1)
(i) Print prime, i
3. Stop.

Algorithm: prime(i)

1.
2.
3.
4.
5.
6.
7.
8.

p=sqrt(i).
j=2,l=1
while(j<=p)( repeat 3 through 7)
r=i%j
if(r==0) then no 6, else step 7.
l=0, goto step 8.
j++.
return(l)

Flowchart:
start

Input n

No

For i=1 to n

yes
k=prime(i)

No

If
k==1

Yes
Print i

stop

Prime(i)
p=sqrt(i)
j=2,l=1

While(j<=p)

True
r=i%j

No

If r==0

j++

Yes
l=0
return(l)

False

Exercise 4:
Write a C Program to find the roots of a quadratic equation.
Algorithm:

1) Enter the values for a, b, c;


2) Calculate discriminate d = b2 4ac.
3) If discriminate is 0, then choice is 1, if d is greater than 0 then choice is 2
otherwise choice is 3.
4) Print a, b, c, d.
5) Choose the sequence based on choice = 1 step 6, choice = 2 goto step 7,
choice = 3 goto step 8.
6)
i) print Equal roots
ii) x1=-b/2*a
iii) x2 = -b/2 * a.
iv) print the roots x1, x2.
v) goto step 9.
7)
i)
print roots are real and unequal.
ii)
x1 = -b + d / 2a
iii) x2 = - b - d / 2a
iv) print x1, x2
v)
goto 9.
8) i) print roots are imaginary
ii) print x1 ie complex root.
iii) print x2 ie complex root.
9) Stop.

Flowchart:
Start

Enter values a, b, c
d= b2 - 4 ac

Yes

ch = 1

If
d=0

No

No

If
d>0

Yes

ch = 2

ch = 3

Print a, b, c, d

Switch
case ch

1
Print Equal roots

2
Print roots are real
& unequal

3
Print Imaginary
roots

x1 = x2 = - b / 2a
x1 = - b+d/2a
Print x1, x2

Print x1 is
complex

x2=-b-d/2a

Print x1, x2

Stop

Print x2 is
complex

Exercise 6:
Write a C Program, which takes two integers operands and one operator from the user,
performs the operation and prints the result. (Consider the operators +,-,*,/,% and use Switch
Statement ).
Algorithm:
1. Begin
2. Enter a, b values.
3. Print MENU.
(i)
Print + Addition.
(ii)
Print - Subtraction.
(iii)
Print * Multiplication.
(iv)
Print / Division.
(v)
Print % Remainder.
(vi)
Print E Exit.
4. Print Enter your choice.
5. If op==E then goto step 8 otherwise follow the below steps
6. Switch(op)
a. case +:
i. Print Addition.
ii. c=a+b.
iii. Print Sum=c.
iv. break
b. case -:
v. Print Subtraction.
vi. c=a-b.
vii. Print Difference=c.
viii. break
c. case *:
ix. Print Multiplication.
x. c=a*b.
xi. Print Product=c.
xii. break
d. case /:
xiii. Print Division.
xiv.
c=a/b.
xv.
Print Quotient=c.
xvi.
break
e. case %:
xvii. Print Remainder.
xviii. c=a%b.
xix.
Print Remainder=c.
xx. break
f. default:
xxi.
Print Invalid Option.
xxii. break
7. while(1) then goto step 3.
8. Stop.

Flowchart:
Start

Input a, b
Menu
+ Addition
- Subtraction
* Multiplication
/ Division
% Remainder
E Exit
Input choice op
Yes

If op = E
No
Switch
(OP)
+

c=a+b

c=ab

c=a*b

Print
sum c

Print
Diff c

Print
product c

Stop

c=a/b

Print
Quaff c

c=a%b

Print
remainder c

Exercise 7:
Write a C program that uses functions to perform the following operations.
i) To insert a sub-string into given main string from a given position.
Algorithm:
1. Enter the main string: s
2. Determine length of string: s i.e. l
3. Print the main string: s
4. Input position of the substring to be inserted: p
5. if p is ve or p is greater than length of main string (l), then goto step 6 otherwise goto step 7 .
6. print ie substring out of the main string position, goto step 13.
7. input the substring, s1.
8. l1=string length of s1.
9. if l + l1 > L then go to step 10 otherwise go to step 11.
10. substring cant be inserted, because it is too long goto step 13.
11. call insert(s, p, s1)
12. print After string insertion the main string is: s.
13. stop.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define L 80
#define P 40
main()
{
int p,l,i,l1;
char s[L],s1[P];
void inst(char s[],int p,char s1[]);
clrscr();
printf("Enter the Main string:\n");
scanf("%[^\n]",s);
l=strlen(s);
printf("The main string is \n%s",s);
printf("\nPosition of the substring to be inserted:\n");
scanf("%d",&p);

if((p<0)||(p>l))
printf("Substring position is out of the main string.");
else { getchar();
printf("Enter substring:\n");
scanf("%[^\n]",s1);
l1=strlen(s1);
if((l+l1)>L)
printf("\nstring can't be inserted because too long.");
else
{
inst(s,p,s1);
printf("\nAfter string insertion:\n");
printf("%s",s);
}
}
getch();
}/*End of main functioin*/

Algorithm: Insert(s, p, s1)


1.l = length of s.
2. i=p;
3. k=length s1.
4. l1 = k
5. last = k+l+1
6. s[last] = \0
7. last = last 1.
8. p1 = l (p - 1)
9. s[last] = s[l]
10.last= last 1
11. l = l 1
12. p1 = p1 1
13. if(p!=0) then repeat 6 through 14.
14. k = 0
15. repeat i = p to i<=p + l1
i) s[i] = s1[k]
ii) k = k + 1.
16. return.

void inst(char s[],int p,char s1[])


{
int l1,last,p1,i,j,l,k;
l=strlen(s);
i=p;
k=strlen(s1);
l1=k;
last=k+l+1;
s[last]='\0';
last--;
p1=l-(p-1);
do{
s[last]=s[l];
last--;
l--;
p1--;
}
while(p1!=0);
k=0;
for(i=p;i<p+l1;i++)
{
s[i]=s1[k];
k++;
}/*End of for loop*/
return;
}/*End of inst function*/

Output:
Enter the Main string:
Java is oops language
The main string is
Java is oops language
Position of the substring to be inserted:
8
Enter substring:
an
After string insertion:
Java is an oops language

Flowchart:
Start
Input main string: s

l=strlen(s)
print sub string: s1

Input substring p

Yes

No

If p<0 and p>l

Input substring s1
Print subs teing of a
main string
l1=strlen(s1)

Yes

If l +l1>l

No

Insert(s, p, s1)
Print string cant
be inserted
Print after
insertion: s

Stop

Flowchart: Insert(s, p, s1)


Start
L=strlen(s)
i=p
K=strlen(s1)
Last=k+l+1
S(last)=\0

Last=last - 1
P1 = l (p - 1)

S(last) = s[i]

Last = last 1; l = l - 1

P1 = p1 - 1

Yes

While(p!=0)
N0
K=0
For i=p to p+ l1

No

Yes
S[i]= s1[k]
K=k+1
return

ii) To delete n Characters from a given position in a given string.


Algorithm:
1. Input text line.
2. Determine length of line l.
3. input position of starting character pos.
4. input No of characters to be deleted nc.
5. na = l pos.
6. if nc > na then perform the following otherwise goto step 7.
i) print text deleted from pos to end.
ii) line[pos]=\0.
7. p = pos + nc.
8. k = p.
9. for i=1 to l k perform the following
i) line[pos]=line[p].
ii) pos=pos+1
iii) p = p + 1.
10. line[pos]=\0.
11. print After deleting line is line,s.
12. stop.

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define ML 80
main()
{
char line[ML];
int p,l,n,pos,nc,na,k,i;
clrscr();
printf("Enter the line of text:");
scanf("%[^\n]",line);
l=strlen(line);

printf("Enter the position of the starting character\n");


scanf("%d",&pos);
na=l-pos;
printf("Enter No of characters to be deleted ie should be <=%d",na);
scanf("%d",&nc);
if(nc>na)
{
printf("The text is deleted from %d till the end",pos);
line[pos+1]='\0';
printf("After deletion the string :%s",line);
}
else
{
p=pos+nc;
k=p;
for (i=1;i<=l-k;i++)
{
line[pos]=line[p];
pos++;
p++;
}
line[pos]='\0';
printf("After Deletion the string is :%s",line);
getch();
}
}/*End of main function*/

Output:
1) Enter the line of text: ABJ KALAM IS A FORMER PRECIDENT
Enter the position of the starting character
14
Enter No of characters to be deleted ie should be <=17
18
The text is deleted from 14 till the end.
After deletion the string: ABJ KALAM IS A
2) Enter the line of text: PRASAD BV
Enter the position of the starting character
6
Enter No of characters to be deleted ie should be <=3
2
After Deletion the string is: PRASADV

Flowchart:
Start
Input a line of text line

L=strlen(line)
Input position of selecting character
pos
Input No of characters to be deleted
nc

na = l pos

Yes

If nc > na

No
P=pos + nc
K=p

Print the text is deleted


from pos to end

For i=1 to l - k
Yes
Line[pos] = line[p]

Line[pos+]=\0

Pos = pos + 1
P=p+1

Line[pos] = \0
Print After deletion of a line

stop

No

Exercise 8:
a)Write a C Program to find the factorial of given number use both recursive and
non-recursive functions.
i) To find the factorial of a given integer.
ii) To find the GCD (greatest common divisor) of two given integers.
iii) To solve Towers of Hanoi problem.
Non-Recursive Approach:
Algorithm:
(1) Input a +ve integer No n.
(2) Check whether n is +ve or not.
i)
If n is ve print cant be evaluated the factorial goto step (4).
ii)
Else calculate fact i.e., n! call function fact(n).
(3) Print factorial of n.
(4) Stop.
Algorithm for fact():
(1) f = 1
(2) Repeat i=1 to n times.
f = f * i.
(3) Return f.
Flowchart:

Flowchart: fact(n)
Start

Start

Enter a +ve no n

f=1

for i = 1 to n

Yes

if
n<0

No

Yes
f*=i

f=fact(n)
Print cant
calculate
Print f, n

Stop

Return f

No

Recursive Approach

Algorithm:

FlowChart:
Start
Input n
Print n,
fact(n)
Stop

Flowchart: fact(n)
fact(n)

If n<=1
return(1)
return(n*fact(n-1))

ii. Write a C Program to find GCD (Greatest Common Divisor) of two given integers
use both recursive and non-recursive functions.
Recursive Approach
Algorithm:
Flowchart: main()
Start
Input 2 Nos, a, b

gcd=hcf(a, b)

Print a, b, gcd

Stop

Flowchart: hcf(p, q)

hcf(p, q)
r = p-(p/q2)

If
r==0

No

Yes
return(q)

return(r)

hcf(p, q)

iii. Write a 'C' Program to solve tower of HANOI problem using both
Recursive and Non-Recursive.
Recursive Approach
Algorithm:
Flowchart:
Start

snvalue=L
invalue=C
dnvalue=R

Input Disks, n

Print Tower of Hanoi problem with disks, n

hanoi(n, snvalue, invalue, dnvalue)

Stop

hanoi(n, snvalue, invalue, dnvalue)

If n!=0

No

Yes
hanoi(n-1,sndl,dndl,indl)

Print Move disk n from sndl to dndl


hanoi(n-1, indl, sndl, dndl)

return

Program:
#include<stdio.h>
#include<conio.h>
main()
{
int nvalue;
char snvalue='L',invalue='C',dnvalue='R';
void hanoi();
clrscr();
printf("Enter number of disks: ");
scanf("%d",&nvalue);
printf("\nTower of Hanoi problem with %d disks \n,nvalue");
hanoi(nvalue,snvalue,invalue,dnvalue);
printf("\n");
getch();
}
void hanoi(int n,char sndl,char indl,char dndl)
{
if(n!=0)
{
/*Move n-1 disks from starting needle to intermediate needle*/
hanoi(n-1,sndl,dndl,indl);
/*Move disk n from start to destination*/
printf("Move disk %d from %c to %c \n",n,sndl,dndl);
/*Move n-1 disks from intermediate needle to destination needle*/
hanoi(n-1,indl,sndl,dndl);
}
return;
}
Output:
Enter number of disks: 3
Tower of Hanoi problem with 3 disks
Move disk 1 from L to R
Move disk 2 from L to C
Move disk 1 from R to C
Move disk 3 from L to R
Move disk 1 from C to L
Move disk 2 from C to R
Move disk 1 from L to R

Exercise 11:
b) Write a C function that uses functions to perform the following:
i. Write a C program that displays the position or index in the string S where the
string T begins or -1 if S doesnt contain T.
Algorithm:
1.
2.
3.
4.

Input first string s.


Input second string t (to be searched).
Search t is ie found = strstr(s, t).
If found then (i) else (iii).
(i)
Print t is found is s.
(ii)
Goto step 5.
(iii)
Print t is not found.
5. Stop.
Flowchart:
Start

Input first string s

Input enter string to be searched t

Search t in s is found = strstr(s, t)

If found

Yes
Print string is
found

Stop

No

Print not
found

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define L 80
#define M 40
main()
{
char s[L],t[M];
char *found;
clrscr();
printf("Enter the string S ie first one\n");
scanf("%[^\n]",s);
getchar();
printf("Enter the string T to be searched ie end string\n");
scanf("%[^\n]",t);
found=strstr(s,t);
if(found)
printf("%s string found in %s string",t,s);
else
printf("Not found:-1");
getch();
}
Output:
i) Enter the string S ie first one
HELLO HOW ARE YOU
Enter the string T to be searched ie end string
L
L string found in HELLO HOW ARE YOU string

ii) Enter the string S ie first one


HI HOW ARE YOU, THIS IS PRASAD.
Enter the string T to be searched ie end string
N
Not found:-1

ii. Write a C program to count the lines, words and characters in a given text.
Algorithm:
1. nw = na = nc = 0.
2. Repeat the following until no character is empty.
i.

Input a line of text.

ii.

Determine the length of a line is c.

iii.

if c = 0 then goto step 3 otherwise goto step iv.

iv.

for i = 0 to c repeat the following


a. Check line[i] for space or end of line[\0].
If space or end of line then nw = nw + 1, otherwise goto step 6.
b. nl = nl + 1.

3. Print no of lines nl.


4. Print no of words nw.
5. Print no of characters nc.

6. Stop.

Flowchart:
Start

nw = ne = nl = 0

While (1)
True
Input line

c = strlen(line)
Yes
If c= =0
No
nc = nc + c
No

for i = 0 to c
Yes

If line[i]=space
or line[i]=\0

No

Yes
nw = nw + 1

nl = nl + 1

Print no of lines nl, no of characters c, no of words nw


Stop

Program:
#include<stdio.h>;
#include <ctype.h>c
#include<conio.h>
#include<string.h>
#define L 80
main()
{
char line[L];
int ch,i,n,nw,nc,nl,c;
clrscr();
nw=nc=nl=0;
printf("Enter the text, leave one space");
printf(" between the words and press enter at the end of the text.\n");
while(1)
{
getchar();
printf("enter string OR press enter to stop\n");
scanf("%[^\n]",line);
Output:
c=strlen(line);
if(c==0)break;
Enter the text, leave one space between the words
else
and press enter at the end of the text.
nc+=strlen(line);
for(i=0;i<=c;i++)
if(isspace(line[i])||(line[i]=='\0')) Enter string OR press enter to stop
CAN I SPEAK TO Mr.PRASAD.
nw++;
Enter string OR press enter to stop
nl++;
YA THIS IS PRASAD.
strset(line,' ');
Enter string OR press enter to stop
} /*end of while*/
MAY I KNOW HOW R U
Enter string OR press enter to stop
/*clrscr();*/
THIS IS RAMU.
printf("\nNo of lines:%d",nl);
Enter string OR press enter to stop
printf("\nNo of charactors:%d",nc);
printf("\nNo of words:%d",nw);
getch();
No of lines:4
}
No of charactors:74
No of words:18

Exercise 12:
a) Write a C program to generate Pascals triangle.
Algorithm
Algorithm: main()
1.
2.
3.
4.
5.

Input size of the Pascal Triangle rows / columns m.


Call the function ps(pas, m) (to generate Pascal Triangle).
Print Pascal Triangle.
Call prps(pas, m).
Stop.

Algorithm: ps(pas, m)
1.
2.
3.
4.
5.
6.
7.
8.

For i=0 to m
Pas[i][0] = 1.
Pas[i][i] = 1.
For j = 0 to i 1.
Pas[i][j] = pas[i-1][j-1] + pas[i-1][j].
Next j.
Next i.
Return.

Algorithm: prps()
1.
2.
3.
4.
5.
6.

For I = 0 to m.
For j = 0 to i.
Print pas[i][j].
Next j.
Next i.
Return.

Flowchart: main()

Flowchart: prps(pas, m)
Start

Start

For I = 0, I to m

Input No of Rows columns

False

True
Ps(pas, m)

For j = 0 to i
True

Print Pascal Triangle

Print pas [i][j]

Prps(pas, m)

Stop

return

Flowchart: ps(pas, m)

Start

for i = 0 to m

False

True
pas[i][0] = 1; pas[i][i] = 1

for j = 1 to i -1

False

True
Pas[i][i] = pas[i-1][j-1] + pas[i-1][j]

return

False

Program:
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#define MR 20
#define ML 30
main()
{
int m,n,pas[MR][ML];
void ps(int pas[][ML],int m);
void prps(int pas[][ML],int m);
clrscr();
printf("Enter No of Rows/Columns");
scanf("%d%d",&m,&n);
ps(pas,m);
printf("\nPascal Triangle\n");
prps(pas,m);
getch();
}/*end of main function*/
void ps(int pas[][ML],int m)
{
int i,j;
for(i=0;i<m;i++)
{
pas[i][0]=1;
pas[i][i]=1;
for(j=1;j<i;j++)
pas[i][j]=pas[i-1][j-1]+pas[i-1][j];
}/*end of for loop*/
return;
}/*end of the function*/
void prps(int pas[][ML],int m)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<=i;j++)
printf("%3d",pas[i][j]);
printf("\n");
}
return;
}/*end of functi on*/

Output:
Enter No of Rows/Columns5 5
Pascal Triangle
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1

b) Write a C program to construct a Pyramid of numbers.


Algorithm:
1. Begin.
2. For i=1 to 10 repeat the following steps
(i)
K=1
(ii)
For s=1 to 10-I repeat the following steps
Print
.
(iii)
For j=1 to I repeat the following steps
(a) R=k%10.
(b) Print r.
(c) K=k+1.
(iv)
K=k-1.
(v)
For j=1 to I repeat the following steps
(a) K=k-1
(b) If k==-1 then
K=10
(c) R=k%10
(d) Print r.
(vi)
Goto next line.
3. Stop.

Flowchart:
Start

False

for i=1 to 10
True
k=1
for s=1 to 10-i

False

True
Print

for j=1 to i

False

True
r=k%10
Print r
k=k+1

k=k-1
False

for j=1 to i

True
k=k+1
If k==-1
No
k=10
yes

r=k%10
Print r
Print \n
Stop

Program:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
main()
{
int i,j,k,r,s;
clrscr();
for(i=1;i<=10;i++)
{
k=1;
for(s=1;s<=10-i;s++) printf(" ");
for(j=1;j<=i;j++)
{
r=k%10;
printf("%d",r);
k++;
}/*end of for loop2*/
k--;

for(j=1;j<i;j++)
{
k--;
if(k==-1) k=10;
r=k%10;
printf("%d",r);
}/*end of for loop3*/
printf("\n");
}/*end of for loop1*/
getch();
}/*end of main function*/

Output:
1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567890987654321

Exercise 16:
a) Write a C program which copies one file to another.

Algorithm:
1. Input the program source destination file.
As command line prompt (argv)
2. Check no of parameters ie argc
If argc !=3 goto step 10
3. Copy source file name into sfname
4. Copy destination file into dfname.
5. Open the file ie source file in read mode.
6. Check source file opening of file .
If not, print file cant be opened goto step 10, otherwise goto step 7.
7. Perform till the end of the file reading the following
(i)

Read a character from a source file.

(ii)

Write to destination file.

8. Close source file.


9. Close destination file.
10. Stop.

Flowchart:
Start
C:\> filename sfile
dfile
If argc!=3

Yes

No

Copy argv[1] to sfile


Copy argv[2] to dfile
Open sfile in read
mode
If sfpt==NULL

Yes

No

Open dfile

If dfpt==NULL
Yes
No

While !feop(sfpt)
No

Read c from
Yessfpt
Write c into dfpt

Close sfile
Close dfile

Stop

Program:
#include<stdio.h>
#include<string.h>
#include<dos.h>
#include<conio.h>
#define L 80
main(int argc, char *argv[])
{
FILE *sfpt,*dfpt;
char c;
char sfname[L],dfname[L];
clrscr();
if(argc!=3)
{
printf("Invalid No of parameters.");
exit(1);
}
strcpy(sfname,argv[1]);
strcpy(dfname,argv[2]);
sfpt=fopen(sfname,"r");
if(sfpt==NULL)
printf("File can't be opend %s",sfname);
else
{
dfpt=fopen(dfname,"w");
if(dfpt==NULL)
printf("File can't be found: %s",sfname);
else
{
while(!feof(sfpt))
{
c=getc(sfpt);
putc(c,dfpt);
}
}
}
fclose(sfpt);
fclose(dfpt);
getch();
}

b) Write a C program to reverse the first n characters in a file. (Note: The file name and n
are specified on the command line)
Algorithm:
1. Input C:\> filename, sourcefile.
2. if argc!=3 then goto step (6)
3. Copy argv[1] to sfname
4. Open sfname in read mode
5. if sfpt = = NULL
6. Print file not opened, otherwise goto step (7)
7. Convert string to integer ie i = a to i(argv[2])
8. Read source file buffer ie sfpt into text up to I no of characters.
9. text[n] = \0( NULL Character assignment)
10. j = 1
11. rev[j] = \0
12. j = j 1
13. for i = 0 to l
a. rev [j] = text[i]
b. j = j l
14. Print reversed string : rev
15. Close file sfpt
16. Stop.

Flowchart:
Start

C:\> filename sfile


dfile
if argc!=3

Yes

No
Copy argv[1] to sfname

Open sfname in read


mode
if sfpt==NULL

Convert argv[2]
No to
integer

Yes

Print file not


found

n = fread(text, l, sfpt)

text[n]=\0
l=length(text)
j=1
L=length(text)

rev[j]=\0
j=j+
1

False

L=length(text)
for
i=0;i<l;i++
True

rev[i] = text[i]

Print reversed
string rev

j--

Stop

Program:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<dos.h>
#include<stdlib.h>
#define L 800
main(int argc, char *argv[])
{
int l,i,j;
char text[L];
char rev[L];
FILE *sfpt;
char c;
char sfname[L];
int n;
clrscr();
if(argc!=3)
{
printf("Invalid No of arguments.\n");
exit(0);
}
strcpy(sfname,argv[1]);
sfpt=fopen(sfname,"r");
if(sfpt==NULL)
{
printf("File not found.\n");
exit(1);
}
else{
i=atoi(argv[2]);
n=fread(text,1,i,sfpt);
text[n]='\0';
l=strlen(text);
j=l;
rev[j]='\0';
j--;
for(i=0;i<l;i++)
{
rev[j]=text[i];
j--;
}
printf("Reversed String :\n %s",rev);
fclose(sfpt);
}
/*getch();*/
}

This page is intentionally left blank

You might also like