0% found this document useful (0 votes)
281 views

Programming in C Lab Manual

This document contains a C programming lab manual with examples of programs to perform basic calculations and operations. The examples cover topics like simple and compound interest calculation, swapping variables with and without a third variable, finding the biggest of three numbers, checking for palindromes, converting case of characters in a string, creating a student mark list using structures, and calculating sum and average using functions. For each example, the aim, algorithm, program code, sample input/output and result is provided. The document serves as a guide for learning basic C programming concepts through examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
281 views

Programming in C Lab Manual

This document contains a C programming lab manual with examples of programs to perform basic calculations and operations. The examples cover topics like simple and compound interest calculation, swapping variables with and without a third variable, finding the biggest of three numbers, checking for palindromes, converting case of characters in a string, creating a student mark list using structures, and calculating sum and average using functions. For each example, the aim, algorithm, program code, sample input/output and result is provided. The document serves as a guide for learning basic C programming concepts through examples.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

C Programming Lab Manual

PART - A
Ex. No.: A1 SIMPLE AND COMPOUND INTEREST
Date:

Aim:
To write a program to calculate simple and compound interest.

Algorithm:
1. Start.
2. Read the values for principal amount (p), no. of years (n) and rate of interest (r).
3. Calculate si = (p*n*r)/100.
4. Calculate ci = p*(1+r/100)n - p.
5. Print the values of si and ci.
6. Stop.

Program:
#include <stdio.h>
#include <conio.h>
#include <math.h>

void main()
{
int n,r;
float p,si,ci;

clrscr();

scanf("%f %d %d", &p,&n,&r);

si = (p*n*r)/100;
ci = p*pow((1.0+r/100.0),n) - p;

printf("\n Simple Interest = %f",si);


printf("\n Compound Interest = %f",ci);

getch();
}

Input:
1000.00
20
6

Output:
Simple Interest = 1200.000000
Compound Interest = 2207.135498

Result:
Thus, the program to calculate simple and compound interest was written, compiled and executed.
_____________________________________________________________________________________________

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Ex. No.: A2a SWAPPING TWO VARIABLES USING THIRD VARIABLE


Date:

Aim:
To write a program to swap two variables using a third variable.

Algorithm:
1. Start.
2. Read the values for a and b.
3. Print the values of a and b before swap.
4. Swap the values of a and b using third variable, temp.
5. Print the values of a and b after swap.
6. Stop.

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

void main()
{
int a,b,temp;

clrscr();

scanf("%d %d",&a,&b);
printf("a=%d b=%d",a,b);

temp = a;
a = b;
b = temp;

printf("\na=%d b=%d",a,b);

getch();
}

Input:
4
7

Output:
a=4 b=7
a=7 b=4

Result:
Thus, the program to swap two variables using a third variable was written, compiled and executed.
_____________________________________________________________________________________________

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Ex. No.: A2b SWAPPING TWO VARIABLES WITHOUT USING THIRD VARIABLE
Date:

Aim:
To write a program to swap two variables without using a third variable.

Algorithm:
1. Start.
2. Read the values for a and b.
3. Print the values of a and b before swap.
4. Swap the values of a and b using the following formula:
a=a-b
b=b+a
a=b-a
5. Print the values of a and b after swap.
6. Stop.

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

void main()
{
int a,b;

clrscr();

scanf("%d %d",&a,&b);
printf("a=%d b=%d",a,b);

a = a - b;
b = b + a;
a = b - a;

printf("\na=%d b=%d",a,b);

getch();
}

Input:
6
4

Output:
a=6 b=4
a=4 b=6

Result:
Thus, the program to swap two variables without using a third variable was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: A3 BIGGEST OF THREE NUMBERS


Date:
3

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Aim:
To write a program to find the biggest of three numbers.

Algorithm:
1. Start.
2. Read the values for variables a, b and c.
3. If a > b
a. If a > c, print a as the biggest number.
b. Else, print c as the biggest number.
4. Else
a. If b > c, print b as the biggest number.
b. Else, print c as the biggest number.
5. Stop.

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

void main()
{
int a,b,c;

clrscr();

scanf("%d %d %d",&a,&b,&c);

if(a>b)
{
if(a>c)
printf("\n Big=%d",a);
else
printf("\n Big=%d",c);
}
else
{
if(b>c)
printf("\n Big=%d",b);
else
printf("\n Big=%d",c);
}

getch();

Input:
7
8
6

Output:
Big=8

Ex. No.: A4 PALINDROME


Date:
4

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Aim:
To write a program to check whether a given string is a palindrome or not.

Algorithm:
1. Start.
2. Read the value for string a.
3. Copy the value of string a to string b.
4. Reverse string b.
5. If a = b, print that the given string is a palindrome.
6. Else, print that the given string is not a palindrome.
7. Stop.

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

void main()
{
char a[10],b[10];

clrscr();

scanf("%s",a);

strcpy(b,a);
strrev(b);

if(strcmp(a,b)==0)
printf("\n Palindrome");
else
printf("\n Not palindrome");

getch();
}

Input:
DAD

Output:
Palindrome

Ex. No.: A5 LOWERCASE TO UPPERCASE AND VICE VERSA


Date:

Aim:
5

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

To write a program to convert lower case characters in a string to uppercase and vice versa.

Algorithm:
1. Start.
2. Read the value for string a.
3. Repeat the following steps for each character in string a (a[i]):
a. If a[i] is an uppercase character, convert it to lowercase and store in b[i].
b. Else, convert the lowercase character in a[i] to uppercase and store in b[i].
4. Print the converted string b.
5. Stop.

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

void main()
{
int i;
char a[10],b[10];

clrscr();

scanf("%s",a);

for(i=0;a[i]!='\0';i++)
{
if(isupper(a[i]))
b[i] = tolower(a[i]);
else
b[i] = toupper(a[i]);
}

b[i] = '\0';

printf("\n %s",b);

getch();
}

Input:
Velu

Output:
vELU

Ex. No.: A6 MARK LIST USING ARRAY OF STRUCTURES


Date:

Aim:
6

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

To write a program to prepare a mark list of students using array of structures.

Algorithm:
1. Start.
2. Define the student structure with regno, name, mark1, mark2 and mark3 as members.
3. Repeat the following step for each student:
a. Read the regno, name, mark1, mark2 and mark3.
4. Print RegNo., Name, Mark1, Mark2 and Mark3 as column titles.
5. Repeat the following step for each student:
a. Print the regno, name, mark1, mark2 and mark3.
6. Stop.

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

struct student
{
int regno;
char name[20];
int mark1;
int mark2;
int mark3;
};

void main()
{
int i;
struct student s[2];

clrscr();

for(i=0;i<2;i++)
{
scanf("%d %s %d %d %d",&s[i].regno,s[i].name,&s[i].mark1,&s[i].mark2,&s[i].mark3);
}

printf("RegNo. \t Name \t Mark1 \t Mark2 \t Mark3");


for(i=0;i<2;i++)
{
printf("\n %d \t %s \t %d \t %d \t %d",s[i].regno,s[i].name,s[i].mark1,s[i].mark2,s[i].mark3);
}

getch();
}

Input:
1
Velu
70
80
90
2
Samy
50
60
7

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

70

Output:
RegNo. Name Mark1 Mark2 Mark3
1 Velu 70 80 90
2 Samy 50 60 70

Result:
Thus, the program to prepare a mark list of students using array of structures was written, compiled and
executed.
_____________________________________________________________________________________________

Ex. No.: A7 SUM AND AVERAGE OF THREE NUMBERS USING FUNCTION


Date:

Aim:
To write a program to calculate sum and average of three numbers using function.

Algorithm:
1. Start.
2. Define the function to calculate sum and average of three numbers.
3. Read values for a, b and c.
4. Calculate sum as sum = a + b + c.
5. Calculate average as avg = sum/3.
6. Print sum and average.
7. Stop.

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

void f();

void main()
{
clrscr();
f();
getch();
}

void f()
{
int a,b,c,sum;
float avg;

scanf("%d %d %d",&a,&b,&c);

sum = a+b+c;
avg = sum/3.0;

printf("\n Sum=%d",sum);
printf("\n Average=%f",avg);

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Input:
2
4
6

Output:
Sum = 12
Average = 4.0

Result:
Thus, the program to calculate sum and average of three numbers using function was written, compiled and
executed.
_____________________________________________________________________________________________

Ex. No.: A8 LENGTH OF A STRING USING POINTER


Date:

Aim:
To write a program to find the length of a string using pointer.

Algorithm:
1. Start.
2. Read the value for string a.
3. Assign the address of a[0] to pointer p.
4. Repeat the following steps while *p != ‘\0’:
a. Increment string length, l.
b. Increment pointer, p.
5. Print the string length, l.
6. Stop.

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

void main()
{
char a[10];
char *p;
int l=0;

clrscr();

scanf("%s",a);
p = &a[0];

while(*p != '\0')
{
l++;
p++;
}

printf("\n String Length=%d",l);

getch();
}

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Input:
Velu
Output:
String Length = 4

Result:
Thus, the program to find the length of a string using pointer was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: A9 ADDRESS AND VALUE OF A VARIABLE USING POINTER


Date:

Aim:
To write a program to print the address and value of a variable using pointer.

Algorithm:
1. Start.
2. Read the value for variable a.
3. Assign the address of a to pointer p.
4. Print the address of the variable (p).
5. Print the value of the variable (*p).
6. Increment the value of the variable by 5.
7. Print the new value of the variable (*p).
8. Stop.

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

void main()
{
int a;
int *p;

clrscr();

scanf("%d",&a);
p = &a;

printf("\n Variable Address = %ld",p);

printf("\n Old Variable Value = %d",*p);


*p=*p+5;
printf("\n New Variable Value = %d",*p);

getch();
}

Input:
10
Output:
Variable Address = 85524468
Old Variable Value = 10
New Variable Value = 15

Result:
10

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Thus, the program to print the address and value of a variable using pointer was written, compiled and
executed.
_____________________________________________________________________________________________
Ex. No.: A10 SWAP DATA VALUES USING MACRO
Date:

Aim:
To write a program to swap two data values using macro.

Algorithm:
1. Start.
2. Define a macro to swap two data values using a third variable.
3. Read the values for a and b.
4. Print the values of a and b before swap.
5. Swap the values of a and b using macro.
6. Print the values of a and b after swap.
7. Stop.

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

#define swap(x,y) (int t;t=x;x=y;y=t;)

void main()
{
int a,b,t;

clrscr();

scanf("%d %d",&a,&b);

printf("\n Before swap: a=%d b=%d",a,b);


swap(a,b);
printf("\n After swap: a=%d b=%d",a,b);

getch();
}

Input:
4
6

Output:
Before swap: a=4 b=6
After swap: a=6 b=4

Result:
Thus, the program to swap two data values using macro was written, compiled and executed.
_____________________________________________________________________________________________

PART – B
Ex. No.: B1 SUM OF DIGITS IN A NUMBER
Date:

11

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Aim:
To write a program to find the sum of digits in a number and also to print the number in reverse order.

Algorithm:
1. Start.
2. Read the value of number.
3. Repeat the following steps while number > 0:
a. Find the individual digit as digit = number%10.
b. Print the digit.
c. Calculate sum as sum = sum + digit.
d. Re-calculate the value of number as number = number/10.
4. Print the value of sum.
5. Stop.

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

void main()
{
int number,digit,sum;
sum = 0;

clrscr();

scanf("%d",&number);

while(number>0)
{
digit = number%10;
printf("%d", digit);

sum = sum + digit;


number = number/10;
}

printf("\n Sum=%d",sum);

getch();
}

Input:
786

Output:
687
Sum=21

Result:
Thus, the program tofind the sum of digits in a number and to print the number in reverse order was
written, compiled and executed.
_____________________________________________________________________________________________

12

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Ex. No.: B2 PRINT WORD FOR NUMBER USING SWITCH STATEMENT


Date:

Aim:
To write a program to print the word for a given number using switch statement.

Algorithm:
1. Start.
2. Read the value of number.
3. Using switch statement print the word corresponding to the number.
4. Stop.

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

void main()
{
int number;

clrscr();

scanf("%d",&number);

switch(number)
{
case 1:
printf("\n ONE");
break;
case 2:
printf("\n TWO");
break;
case 3:
printf("\n THREE");
break;
case 4:
printf("\n FOUR");
break;
case 5:
printf("\n FIVE");
break;
case 6:
printf("\n SIX");
break;
case 7:
printf("\n SEVEN");
break;
case 8:
printf("\n EIGHT");
break;
case 9:
printf("\n NINE");
break;
default:
13

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

printf("\n Enter correct number.");


}
getch();
}

Input:
6

Output:
SIX

Result:
Thus, the program to find the word for a given number using switch statement was written, compiled and
executed.
_____________________________________________________________________________________________

Ex. No.: B3a FACTORIAL OF A NUMBER WITHOUT RECURSION


Date:

Aim:
To write a program to find the factorial of a number without recursion.

Algorithm:
1. Start.
2. Read the value of number.
3. Set fact = 1.
4. Repeat the following step for i = 1 to n:
a. fact = fact * i.
5. Print the value of fact.
6. Stop.

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

void main()
{
int number,fact,i;
fact=1;

clrscr();

scanf("%d",&number);

for(i=1;i<=number;i++)
{
fact=fact*i;
}

printf("\n Factorial = %d",fact);

getch();
}

Input:
4
14

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Output:
Factorial = 24

Result:
Thus, the program to find the factorial of a number without recursion was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B3b FACTORIAL OF A NUMBER WITH RECURSION


Date:

Aim:
To write a program to find the factorial of a number with recursion.

Algorithm:
1. Start.
2. Read the value of number.
3. Call the fact() function and store the returned value in f.
4. Print the value of f.
5. Stop.

Function:
1. If n <= 1, return 1.
2. Else, return n*fact(n-1).

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

void main()
{
int number,f;
int fact(int);

clrscr();

scanf("%d",&number);

f = fact(number);

printf("\n Factorial = %d",f);

getch();
}

int fact(int n)
{
if(n<=1)
return 1;
else
return (n*fact(n-1));
}

Input:
4

15

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Output:
Factorial = 24

Result:
Thus, the program to find the factorial of a number with recursion was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B4 SORTING NAMES


Date:

Aim:
To write a program to sort n names in alphabetical order.

Algorithm:
1. Start.
2. Read the value of n.
3. Repeat the following step for i = 0 to n-1:
a. Read the value for the string, name[i].
4. Repeat the following steps for i = 0 to n-1:
a. Repeat the following steps for j = i+1 to n-1:
i. If name[i] > name[j], exchange values of name[i] and name[j].
5. Repeat the following step for i = 0 to n-1:
a. Print the value of name[i].
6. Stop.

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

void main()
{
char name[10][10],temp[10];
int n,c,i,j;

clrscr();

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",name[i]);

for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
c=strcmp(name[i],name[j]);
if(c>0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}

for(i=0;i<n;i++)
16

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

printf("\n %s",name[i]);

getch();
}

Input:
3
Murugan
Kandan
Gugan

Output:
Gugan
Kandan
Murugan

Result:
Thus, the program to sort n names in alphabetical order was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B5 REMOVING SUBSTRING FROM STRING


Date:

Aim:
To write a program to remove a substring from a given string.

Algorithm:
1. Start.
2. Read the value of the string, s1.
3. Read the value of the substring, s2.
4. Repeat the following steps while pch != NULL:
a. Find the address of s2 in s1 and store it in pch.
b. Copy pch+strlen(s2) to s3.
c. Copy pch to s3.
5. Print the value of s1.
6. Stop.

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

void main()
{
char s1[20],s2[20],s3[3];
char *pch;
int i;

clrscr();

gets(s1);
gets(s2);

do
{
pch = strstr(s1,s2);
17

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

strcpy(s3,pch+strlen(s2));
strcpy(pch,s3);
}
while(pch);

puts(s1);

getch();
}

Input:
Subramanian
ram

Output:
Subanian

Result:
Thus, the program to remove a substring from a given string was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B6 PRINTING ARRAY VALUES USING POINTER


Date:

Aim:
To write a program to print array values using pointer.

Algorithm:
1. Start.
2. Repeat the following step for i = 0 to 9:
a. Read the value of a[i].
3. Store the address of a[0] in pointer, p.
4. Repeat the following step for i = 0 to 9:
a. Print the content of p (*p).
b. Increment p.
5. Stop.

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

void main()
{
int a[10];
int *p;
int i;

clrscr();

for(i=0;i<10;i++)
scanf("%d",&a[i]);

p=&a[0];
for(i=0;i<10;i++)
{
printf("\n %d",*p);
18

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

p++;
}

getch();
}

Input:
0
1
2
3
4
5
6
7
8
9

Output:
0
1
2
3
4
5
6
7
8
9

Result:
Thus, the program to print array values using pointer was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B7a REVERSING A STRING USING POINTER


Date:

Aim:
To write a program to reverse a string using pointer.

Algorithm:
1. Start.
2. Read the value of string a.
3. Find the length of string a and store it in l.
4. Store the address of a[l-1] in pointer p.
5. Repeat the following step for i = 0 to l-1:
a. Print the content of p (*p).
b. Decrement p.
6. Stop.

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

19

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

void main()
{
char a[10];
char *p;
int i,l;

clrscr();

scanf("%s",a);

l=strlen(a);
p=&a[l-1];

for(i=0;i<l;i++)
{
printf("%c",*p);
p--;
}

getch();
}

Input:
RAM

Output:
MAR

Result:
Thus, the program to reverse a string using pointer was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B7b REVERSING ARRAY ELEMENTS USING POINTER


Date:

Aim:
To write a program to reverse array elements using pointer.

Algorithm:
1. Start.
2. Read the value of n.
3. Repeat the following step for i = 0 to n-1:
a. Read the value for a[i].
4. Store the address of a[n-1] in pointer p.
5. Repeat the following step for i = 0 to n-1:
a. Print the content of p (*p).
b. Decrement p.
6. Stop.

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

void main()
{
20

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

int a[10];
int *p;
int i,n;

clrscr();

scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
p=&a[n-1];

for(i=0;i<n;i++)
{
printf("\n %d",*p);
p--;
}

getch();
}

Input:
3

4
5
6

Output:
6
5
4

Result:
Thus, the program to reverse array elements using pointer was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B8 ABBREVIATION OF A NAME


Date:

Aim:
To write a program to print the abbreviation of an organization name.

Algorithm:
1. Start.
2. Read the value of string, name.
3. Find the length of name and store it in l.
4. Set j=0 and k=0.
5. Repeat the following step for i = 0 to l-1:
a. Store name[j] in a[k].
b. If name[i] = ‘ ‘, do the following:
i. Increment k.
ii. Store value of i in j.
iii. Increment j.
iv. Store name[j] in a[k].
6. Print the value of string a.
7. Stop.
21

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

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

void main()
{
char name[50];
char a[5];
int i,j,k,l;

clrscr();

gets(name);

l=strlen(name);
j=0;
k=0;
for(i=0;i<l;i++)
{
a[k]=name[j];
if(name[i] == ' ')
{
k++;
j=i;
j++;
a[k]=name[j];
}
}
a[k+1]='\0';
printf("%s",a);
getch();
}

Input:
Bharath Heavy Electricals Limited

Output:
BHEL

Result:
Thus, the program to print the abbreviation of an organization name was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B9 FILE COPY


Date:

Aim:
To write a program to copy contents of one file to another file and to count the number of characters, words
and lines in the file.

Algorithm:
22

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

1. Start.
2. Open the source file, Data1.txt in read mode and the destination file, Data2.txt in write mode.
3. Set i = 0.
4. Read the first character from the source file into text[i].
5. Repeat the following steps while text[i] != EOF:
a. Write the character in text[i] in the destination file.
b. Increment i.
c. Read the next character from the source file into text[i].
6. Set line = 1, word = 1 and ch = 0.
7. Find the length of string, text and store it in l.
8. Repeat the following step for i = 0 to l-1:
a. Using switch statement count the number of lines, words and characters in string text.
9. Print the values of ch, word and line.
10. Stop.

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

void main()
{
FILE *fp1,*fp2;
char text[100];
int i,l,line,word,ch;

clrscr();

fp1 = fopen("Data1.txt","r");
fp2 = fopen("Data2.txt","w");

i=0;
text[i] = getc(fp1);
while(text[i] != EOF)
{
putc(text[i],fp2);
i++;
text[i] = getc(fp1);
}
text[i] = '\0';

fclose(fp1);
fclose(fp2);

line = 1;
word = 1;
ch = 0;
l = strlen(text);
for(i=0;i<l;i++)
{
switch(text[i])
{
case '\n': line++;
case ' ': word++;
default: ch++;
}
23

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

printf("\n Characters = %d",ch);


printf("\n Words = %d",word);
printf("\n Lines = %d",line);

getch();
}

Input:
Data1.txt: Velu

Output:
Data2.txt: Velu

Characters = 4
Words = 1
Lines = 1

Result:
Thus, the program to copy the contents of one file to another file and to count the number of characters,
words and lines in the file was written, compiled and executed.
_____________________________________________________________________________________________

Ex. No.: B10 PRINTING COMMAND LINE ARGUMENTS IN REVERSE ORDER


Date:

Aim:
To write a program to print command line arguments in reverse order.

Algorithm:
1. Start.
2. Repeat the following step for i = argc-1 to 1 in reverse order:
a. Print the value of argv[i].
3. Stop.

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

void main(int argc, char *argv[])


{
int i;

clrscr();

for(i=argc-1; i>=1; i--)


printf("%s ", argv[i]);

getch();

Input:
Arguments: Sri Adi Chunchanagiri women’s

24

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.


C Programming Lab Manual

Output:
College women’s Chunchanagiri Adi Sri

Result:
Thus, the program to print command line arguments in reverse order was written, compiled and executed.
_____________________________________________________________________________________________

25

Lakshmi.S, Assistant Professor, Sri Adi Chunchanagiri Women’s College, Cumbum.

You might also like