file (394 KB, pdf)

Transcription

file (394 KB, pdf)
C Programming Laboratory [13MCA16]
Acharya Institute of Technology
Department of MCA
C Programming Laboratory
(Sub. Code: 13MCA16)
Lab manual
Prof. D.Samanta, Email: [email protected]
Page 1
C Programming Laboratory [13MCA16]
1
2
3.
4
5
6
7
8
9
a. Convert degrees into Fahrenheit’s and vice versa.
b. Calculate the salary of an employee given his basic
pay, HRA = 10% of basic pay, TA= 5% of his basic
pay and deductions IT = 2.5% of his basic pay.
a. Check whether a number is a perfect number or not.
b. Solve quadratic equations given the value of a, b and
a. Generate all Armstrong numbers up to n.
b. Convert a decimal number to a hexadecimal
number
Write a menu driven C program to
a. Insert an element into an array
b. Delete an element from the array (first
occurrence)
Write a Menu Driven C Program to
a. Accept a string from the user
b. Encode the string.
c. Decode the string
Write a C program to multiply two matrices that
satisfy the constraint of matrix
Multiplication
Write a C program to find the saddle point of a matrix
Write a C program to implement a magic square of
size n.
Write a Menu driven C program to
a. Accept two numbers n and m
b. Sum of all integers ranging from n to m
c. Sum of all odd integers ranging from n to m
d. Sum of all even integers ranging from n to m
Display an error message if n > m. Create functions for
Prof. D.Samanta, Email: [email protected]
Page 2
C Programming Laboratory [13MCA16]
10
11
12
13
14
each of the options
Write a Menu Driven C Program to implement the
following using recursion
a. Factorial of a number
b. Fibonacci series
Create a structure Complex Number having real and
imaginary part as properties.
Write functions to add and subtract the two complex
numbers.
Define a structure called student having the properties
of student_id, student name and branch of the student
with a sub structure of marks of 3 subjects. Write a
Menu Driven C Program to
a. Add new student detail
b. Delete a student detail
c. Display all student details
d. Display the name of the student with the best mark
e. Display the name of the student with the worst mark
f. Display the average marks scored by the students
a. Write a C Program to remove all white spaces and
newline characters from a file.
b. Find whether a given word exists in the file. If it
exists display the location of the word
Write a C program to copy one file content to another
file without using inbuilt functions.
Prof. D.Samanta, Email: [email protected]
Page 3
C Programming Laboratory [13MCA16]
1.A) Convert degrees into Fahrenheit’s and vice versa.
Algorithm:
Code:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
float c,f;
int ch;
clrscr();
do
{
printf("\n Press 1 to convert temparature in fahrenheit :");
printf("\n Press 2 to convert temparature in celcius :");
printf("\n Press 3 for exit :");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\n enter the temparature in fahreinhite\n");
Prof. D.Samanta, Email: [email protected]
Page 4
C Programming Laboratory [13MCA16]
scanf("%f",&f);
c=(5*f-160)/9;
printf("\n the temparature in celcius is=%f",c);
break;
case 2:
printf("\n enter the temparature in celcius\n");
scanf("%f",&c);
f=(9*c+160)/5;
printf("\n the temparature in fahreinhite=%f",f);
break;
case 3: exit(0);
default:
printf("\n You have given a wrong choice:\n");
break;
}
}while(ch!=3);
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 5
C Programming Laboratory [13MCA16]
1.B) Calculate the salary of an employee given his basic
pay, HRA = 10% of basic pay, TA = 5% of his basic pay
and deductions IT =2.5% of his basic pay.
Algorithm:
HRA=0.1*basic_salary;
TA=0.05*basic_salary;
DIT=0.025*basic_salary;
gross_salary=basic_salary+HRA+TA-DIT;
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
float sal,hra,ta,it,basicpay;
clrscr();
printf("\n Enter the basic pay of employee:");
scanf("%f",&basicpay);
hra=(basicpay*10)/100;
ta=basicpay*5/100;
it=basicpay*2.5/100;
sal=(basicpay+hra+ta)-it;
printf("\n The salary of employee is:%f",sal);
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 6
C Programming Laboratory [13MCA16]
2.a) Check whether a number is a perfect number or not.
Algorithm:
The four perfect numbers 6, 28, 496 and 8128 seem to have been
known from ancient times and there is no record of these
discoveries.
6 = 1 + 2 + 3,
28 = 1 + 2 + 4 + 7 + 14,
496 = 1 + 2 + 4 + 8 + 16 + 31 + 62 + 124 + 248,
8128 = 1 + 2 + 4 + 8 + 16 + 32 + 64 + 127 + 254 + 508 + 1016
+ 2032 + 4064
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,c=0;
clrscr();
printf("\nEnter a no:");
scanf("%d",&n);
for(i=1;i<n;i++)
{
if(n%i==0)
{
c=c+i;
Prof. D.Samanta, Email: [email protected]
Page 7
C Programming Laboratory [13MCA16]
}
}
if(n==c)
printf("\nPerfect no.");
else
printf("\nNot a perfect no.");
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 8
C Programming Laboratory [13MCA16]
2.b) Solve quadratic equations given the value
of a, b and c.
Algorithm:
Step1: Accept the value of the coefficient as a, b, c
Step 2: Calculate the discriminate d by the formula
b*b – 4*a*c.
step3: Decision
if d=0 then display real and equal roots
values of roots are –b/2*a and –b/2*a
step 4: if d>0 the display real and unequal roots
values of roots are (-b+√d)/2*a and (-b-+√d)/2*a
step 5: if d<0 then display imaginary and unequal roots
value of roots are (-b/2*a+√di/2*a) and
(-b/2*a - √di/2*a) [ here i= √- 1]
Step 6: Stop
Prof. D.Samanta, Email: [email protected]
Page 9
C Programming Laboratory [13MCA16]
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<math.h>
void main()
{
float a,b,c,d,x,y;
clrscr();
printf("\n Enter the values of a,b,c:\t");
scanf("%f %f %f",&a,&b,&c);
d=(b*b-4*a*c);
if(d==0)
{
printf("\n roots are real and equal");
printf("\n Root1=%f \t Root2=%f",-b/2*a,-b/2*a);
}
if(d>0)
{
printf("\n Roots are real and unequal");
x=((-b+sqrt(d))/(2*a));
y=((-b-sqrt(d))/(2*a));
printf("\n Root1= %f \t Root2=%f",x,y);
}
if(d<0)
{
d=-d;
printf("\n Roots are imaginary and conjugate ");
printf("\n Root1=%f+%fi Root2=%f-%fi",
Prof. D.Samanta, Email: [email protected]
Page 10
C Programming Laboratory [13MCA16]
(-b/2*a),(sqrt(d)/2*a),(-b/2*a),(sqrt(d)/2*a));
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 11
C Programming Laboratory [13MCA16]
3.a. Generate all Amstrong numbers up to n.
Definition of Amstrong number: An Armstrong number of three
digits is an integer such that the sum of the cubes of its digits is
equal to the number itself.
In other word “A number is Amstrong if it is equal the sum of
cube of its digits.”
Example of Amstrong number is 371 because according to
definition cube of its digits sum will be equal to number so
Armstrong number 371=(3)3+(7)3+(1)3
371=27+343+1
371=371
Code:
#include<conio.h>
#include <stdio.h>
void main(){
int r;
long n = 0, i, sum = 0, temp;
printf("Enter an integer upto which you want to find armstrong
numbers\n");
scanf("%ld",&n);
printf("Following armstrong numbers are found from 1 to
%ld\n",n);
for( i = 1 ; i <= n ; i++ )
{
temp = i;
Prof. D.Samanta, Email: [email protected]
Page 12
C Programming Laboratory [13MCA16]
while( temp != 0 )
{
r = temp%10;
sum = sum + r*r*r;
temp = temp/10;
}
if ( i == sum )
printf("%ld\n", i);
sum = 0;
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 13
C Programming Laboratory [13MCA16]
3.b) Convert a decimal number to a hexadecimal number.
Code:
#include<conio.h>
#include<stdio.h>
void main(){
long int dec,r,q;
int i=1,j,temp;
char hexadec[100];
printf("Enter any decimal number: ");
scanf("%ld",&dec);
q = dec;
while(q!=0){
temp = q % 16;
if( temp < 10)
temp =temp + 48;
else
temp = temp + 55;
hexadec[i++]= temp;
q = q / 16;
}
printf("Equivalent hexadecimal value of decimal number %d:
",dec);
for(j = i -1 ;j> 0;j--)
printf("%c",hexadec[j]);
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 14
C Programming Laboratory [13MCA16]
For values of temp less than 10, the appropriate ASCII code
is 48 + temp:
0 => 48 + 0 => '0'
1 => 48 + 1 => '1'
2 => 48 + 2 => '2'
3 => 48 + 3 => '3'
4 => 48 + 4 => '4'
5 => 48 + 5 => '5'
6 => 48 + 6 => '6'
7 => 48 + 7 => '7'
8 => 48 + 8 => '8'
9 => 48 + 9 => '9'
For values 10 or greater, the appropriate letter is 55 + temp:
10 => 55 + 10 => 'A'
11 => 55 + 11 => 'B'
12 => 55 + 12 => 'C'
13 => 55 + 13 => 'D'
14 => 55 + 14 => 'E'
15 => 55 + 15 => 'F'
Prof. D.Samanta, Email: [email protected]
Page 15
C Programming Laboratory [13MCA16]
4. Write a menu driven C program to
a. Insert an element into an array
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], position, c, n, value;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
printf("Enter the location where you wish to insert an
element\n");
scanf("%d", &position);
printf("Enter the value to insert\n");
scanf("%d", &value);
for (c = n - 1; c >= position - 1; c--)
array[c+1] = array[c];
array[position-1] = value;
Prof. D.Samanta, Email: [email protected]
Page 16
C Programming Laboratory [13MCA16]
printf("Resultant array is\n");
for (c = 0; c <= n; c++)
printf("%d\n", array[c]);
getch();
}
b. Delete an element from the array (first occurrence)
Code:
#include <stdio.h>
#include<conio.h>
void main()
{
int array[100], position, c, n;
printf("Enter number of elements in array\n");
scanf("%d", &n);
printf("Enter %d elements\n", n);
for ( c = 0 ; c < n ; c++ )
scanf("%d", &array[c]);
printf("Enter the location where you wish to delete
element\n");
scanf("%d", &position);
Prof. D.Samanta, Email: [email protected]
Page 17
C Programming Laboratory [13MCA16]
if ( position >= n+1 )
printf("Deletion not possible.\n");
else
{
for ( c = position - 1 ; c < n - 1 ; c++ )
array[c] = array[c+1];
printf("Resultant array is\n");
for( c = 0 ; c < n - 1 ; c++ )
printf("%d\n", array[c]);
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 18
C Programming Laboratory [13MCA16]
5. Write a Menu Driven C Program to
a. Accept a string from the user
b. Encode the string.
c. Decode the string
Apply the following procedure to encode it.
1. Convert each character in a string to its ASCI value.
2. Add an integer value to it and display the encoded string
3. Decode the string using reverse procedure and display.
Code:
#include<conio.h>
#include<string.h>
#include <stdio.h>
void main()
{
char name[100],i;
printf("Enter String: ");
scanf("%s",name);
for(i=0;i<strlen(name);i++)
{
name[i]=name[i]+5;
}
for(i=0;i<strlen(name);i++)
{
printf("String after encoding(ASCII+5)\n");
for(i=0;i<strlen(name);i++)
printf("%d\n",name[i]);
}
Prof. D.Samanta, Email: [email protected]
Page 19
C Programming Laboratory [13MCA16]
for(i=0;i<strlen(name);i++)
{
name[i]=name[i]-5;
}
printf("String after Decoding\n");
printf("%s",name);
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 20
C Programming Laboratory [13MCA16]
6. Write a C program to multiply two matrices that satisfy
the constraint of matrix multiplication
Code:
#include<conio.h>
#include<stdio.h>
void main(){
int a[5][5],b[5][5],c[5][5],i,j,k,sum=0,m,n,o,p;
printf("\nEnter the row and column of first matrix");
scanf("%d %d",&m,&n);
printf("\nEnter the row and column of second matrix");
scanf("%d %d",&o,&p);
if(n!=o){
printf("Matrix mutiplication is not possible");
printf("\nColumn of first matrix must be same as row of
second matrix");
}
else{
printf("\nEnter the First matrix->");
for(i=0;i<m;i++)
Prof. D.Samanta, Email: [email protected]
Page 21
C Programming Laboratory [13MCA16]
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("\nEnter the Second matrix->");
for(i=0;i<o;i++)
for(j=0;j<p;j++)
scanf("%d",&b[i][j]);
printf("\nThe First matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<n;j++){
printf("%d\t",a[i][j]);
}
}
printf("\nThe Second matrix is\n");
for(i=0;i<o;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",b[i][j]);
}
}
for(i=0;i<m;i++)
for(j=0;j<p;j++)
c[i][j]=0;
for(i=0;i<m;i++){ //row of first matrix
for(j=0;j<p;j++){ //column of second matrix
sum=0;
for(k=0;k<n;k++)
sum=sum+a[i][k]*b[k][j];
c[i][j]=sum;
}
}
Prof. D.Samanta, Email: [email protected]
Page 22
C Programming Laboratory [13MCA16]
}
printf("\nThe multiplication of two matrix is\n");
for(i=0;i<m;i++){
printf("\n");
for(j=0;j<p;j++){
printf("%d\t",c[i][j]);
}
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 23
C Programming Laboratory [13MCA16]
7. Write a C program to find the saddle point of a matrix.
Logic:
 get the minimum number in the row
 check if the minimum number is the greatest number in it’s
colomn
 repeat the above steps for all rows
Code:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[10][10],i,j,k,n,min,max,col,m;
//clrscr();
printf("enter order m,n of mxn matrix : ");
scanf("%d %d",&m,&n);
printf("enter elements row-wise\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<m;i++)
{
min=a[i][0];
for(j=0;j<n;j++)
Prof. D.Samanta, Email: [email protected]
Page 24
C Programming Laboratory [13MCA16]
{
if(a[i][j]<=min)
{
min=a[i][j];
col=j;
}
}
max=a[0][col];
for(k=0;k<m;k++)
{
if(a[k][col]>=max)
{
max=a[k][col];
}
}
if(max==min)
printf("saddle pt.at (%d,%d)",i+1,col+1);
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 25
C Programming Laboratory [13MCA16]
8. Write a C program to implement a magic square of size n.
Magic square is an ancient mathematical problem that many
people try to solve. May be you see it in some magazines or
your teacher might have introduced it in a class.
Details
A magic square is an arrangement of numbers from 1 to n2 in an
[n x n] matrix, with each number occurring exactly once, and
such that the sum of the entries of any row, any column, or any
main diagonal is the same.
It is not hard to show that this sum must be n [ ( n2 + 1) / 2 ]. If
we use this formula for that example output which is below, for
[5x5] matrix; 5 [ ( 52 + 1 ) / 2 ] = 65.
17
23
4
10
11
65
24
5
6
12
18
65
1
7
13
19
25
65
8
14
20
21
2
65
Prof. D.Samanta, Email: [email protected]
15
16
22
3
9
65
65
65
65
65
65
65
Page 26
C Programming Laboratory [13MCA16]
Code:
#include <stdio.h>
#include<conio.h>
void main() {
// Introductory message
printf("This program creates a magic sqaure of a specified
size.\n");
printf("The size must be an odd number between 1 and 99.\n");
// Get the users magic number and allocate to int n
int n;
printf("Enter size of magic square: ");
scanf("%d", &n);
// Create the array (not using VLA)
int magic[99][99];
int start = (n / 2); // The middle column
int max = n * n; // The final number
magic[0][start] = 1; // Place the number one in the middle of row
0
// Loop to start placing numbers in the magic square
int row;
int col;
int next_row;
int next_col;
int i;
for (i = 2, row = 0, col = start; i < max + 1; i++) {
if ((row - 1) < 0) { // If going up one will leave the top
Prof. D.Samanta, Email: [email protected]
Page 27
C Programming Laboratory [13MCA16]
next_row = n - 1; // Go to the bottom row
}
else { next_row = row - 1; } // Otherwise go up one
printf("In row: %d\n", row);
if ((col + 1) > (n - 1)) { // If column will leave the side
next_col = 0; // Wrap to first column
printf("Column will leave side\n");
}
else { next_col = col + 1; } // Otherwise go over one
printf("In col: %d\n", col);
if (magic[next_row][next_col] > 0) { // If that position is
taken
if (row > (n - 1)) { // If going to row below leaves bottom
next_row = 0; // Go back to the top
}
else {
next_row = row + 1; // Go to the row below
next_col = col; // But stay in same column
}
}
row = next_row;
col = next_col;
printf("About to put %d in row %d, col %d\n", i, row, col);
magic[row][col] = i; // Put the current value in that position
}
// Now let's print the array
int j;
for (i = 0; i < n; i++) {
Prof. D.Samanta, Email: [email protected]
Page 28
C Programming Laboratory [13MCA16]
for (j = 0; j < n; j++) {
printf("%4d", magic[i][j]);
}
printf("\n");
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 29
C Programming Laboratory [13MCA16]
9. Write a Menu driven C program to
a. Accept two numbers n and m
b. Sum of all integers ranging from n to m
c. Sum of all odd integers ranging from n to m
d. Sum of all even integers ranging from n to m
Display an error message if n > m. Create functions for each
of the options
Code:
#include<stdio.h>
#include<conio.h>
#include<process.h>
void main()
{
int sum=0,i,m,n,ch;
while(1){
printf("1.Sum of All numbers \n2.Sum of all even
numbers\n3.Sum of all odd numbers \n4.exit\n");
printf("Enter your choice :");
scanf("%d",&ch);
switch(ch){
case 1:
sum=0;
printf("\nEnter n and m value\n");
scanf("%d %d",&n,&m);
if(m<n)
printf("\n m value should be greater than n\n");
for(i=n;i<=m;i++)
Prof. D.Samanta, Email: [email protected]
Page 30
C Programming Laboratory [13MCA16]
sum=sum+i;
printf("\nSum of %d to %d is %d\n",n,m,sum);
break;
case 2:
sum=0;
printf("\nEnter n and m value \n");
scanf("%d %d",&n,&m);
if(m<n)
printf("\n m value should be greater than n\n");
for(i=n;i<=m;i++)
if(i%2==0)
sum=sum+i;
printf("\nSum of even numbers from %d to %d is
%d\n",n,m,sum);
break;
case 3:
sum=0;
printf("\nEnter n and m value \n");
scanf("%d %d",&n,&m);
if(m<n)
printf("\n m value should be greater than n\n");
for(i=n;i<=m;i++)
if(i%2!=0)
sum=sum+i;
printf("\nSum of odd numbers from %d to %d is
%d\n",n,m,sum);
break;
case 4:
exit(0);
default:
printf("Invalid option\n");
Prof. D.Samanta, Email: [email protected]
Page 31
C Programming Laboratory [13MCA16]
break;
}
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 32
C Programming Laboratory [13MCA16]
10. Write a Menu Driven C Program to implement the
following using recursion
a. Factorial of a number
b. Fibonacci series
Code:
#include<stdio.h>
#include<conio.h>
#include<process.h>
int fact(int n){
if(n==1)
return 1;
return n*fact(n-1);
}
int fibo(int n){
if(n == 0 || n == 1)
return n;
return fibo(n-1) + fibo(n-2);
}
void main() {
int i,j=0,m,n,ch;
while(1){
m=0;
printf("\n1.FACTORIAL OF A NUMBER \n2. FIBONACCI
SERIES \n3.exit\n");
printf("Enter your choice :");
scanf("%d",&ch);
switch(ch){
Prof. D.Samanta, Email: [email protected]
Page 33
C Programming Laboratory [13MCA16]
case 1:
printf("\nEnter n :");
scanf("%d",&n);
m=fact(n);
printf("factorial of a given number is :%d",m );
break;
case 2:
j=0;
printf("\nEnter n :");
scanf("%d",&n);
printf("fibonanci series of a given number is:");
for ( i = 0 ; i < n ; i++ ){
printf("%d\n", fibo(j));
j++;
}
break;
case 3:
exit(0);
default:
printf("Invalid option\n");
break;
}
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 34
C Programming Laboratory [13MCA16]
11. Create a structure Complex Number having real and
imaginary part as properties. Write functions to add and
subtract the two complex numbers.
Code:
#include <stdio.h>
#include<conio.h>
struct complex
{
int real, img;
};
struct complex a, b, c;
void readcom(){
printf("Enter a and b where a + ib is the first complex
number.\n");
printf("a = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second complex
number.\n");
printf("c = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
void addcom(){
c.real = a.real + b.real;
c.img = a.img + b.img;
Prof. D.Samanta, Email: [email protected]
Page 35
C Programming Laboratory [13MCA16]
if ( c.img >= 0 )
printf("Sum of two complex numbers = %d +
%di\n",c.real,c.img);
else
printf("Sum of two complex numbers = %d
%di\n",c.real,c.img);
}
void subcom(){
c.real = a.real - b.real;
c.img = a.img - b.img;
if ( c.img >= 0 )
printf("\nDifference of two complex numbers = %d +
%di",c.real,c.img);
else
printf("\nDifference of two complex numbers = %d
%di",c.real,c.img);
}
void main()
{
readcom();
addcom();
subcom();
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 36
C Programming Laboratory [13MCA16]
12. Define a structure called student having the properties of
student_id, student name and branch of the student with a
sub structure of marks of 3 subjects. Write a Menu Driven C
Program to
a. Add new student detail
b. Delete a student detail
c. Display all student details
d. Display the name of the student with the best mark
e. Display the name of the student with the worst mark
f. Display the average marks scored by the students
Code:
#include <stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
#include<math.h>
typedef struct Student {
char name[20];
char branch[20];
char id[20];
int m1,m2,m3;
float avg;
}Student;
int main(void){
char id[10];
FILE *fp,*ft;
Prof. D.Samanta, Email: [email protected]
Page 37
C Programming Laboratory [13MCA16]
char another,choice;
Student s;
char fname[20];
char lname[20];
long int recsize;
fp=fopen("s.DAT","rb+");
if(fp==NULL) {
fp=fopen( "s.DAT","wb+");
if(fp==NULL) {
printf("Can't Open File");
exit(0);
}
}
recsize=sizeof(s);
while(1) {
printf("\n1.Add Records\n2.Delete Record
\n3.ListRecords\n4.Diaply Average Marks\n5.Exit");
printf("\nEnter your choice :");
fflush(stdin);
scanf("%c",&choice);
switch(choice) {
case'1':
fseek(fp,0,SEEK_END);
printf("\nEnter the id,name branch : ");
scanf("%s %s %s",s.id,s.name,s.branch);
printf("\nEnter the marks1 marks 2, marks 3 :");
scanf("%d %d %d",&s.m1,&s.m2,&s.m3);
s.avg=(s.m1+s.m2+s.m3)/3.0;
fwrite(&s,recsize,1,fp);
break;
case '2':
Prof. D.Samanta, Email: [email protected]
Page 38
C Programming Laboratory [13MCA16]
printf("Enter the id of the Student to be deleted : ");
scanf("%s",&id);
ft=fopen("TEMP.DAT","wb");
rewind(fp);
while(fread(&s,recsize,1,fp)==1) {
if(strcmp(s.id,id)!=0)
fwrite(&s,recsize,1,ft);
}
fclose(fp);
fclose(ft);
remove("s.DAT");
rename("TEMP.DAT","s.DAT");
fp=fopen("s.DAT","rb+");
break;
case '3':
rewind(fp);
while(fread(&s,recsize,1,fp)==1)
printf("\n %s %s %s %d %d
%d",s.name,s.id,s.branch,s.m1,s.m2,s.m3);
break;
case '4' :
rewind(fp);
while(fread(&s,recsize,1,fp)==1)
printf("\n %s %s %s %d %d %d
%f",s.name,s.id,s.branch,s.m1,s.m2,s.m3,s.avg);
break;
case '5':
fclose(fp);
exit(0);
}
}
Prof. D.Samanta, Email: [email protected]
Page 39
C Programming Laboratory [13MCA16]
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 40
C Programming Laboratory [13MCA16]
13. a. Write a C Program to remove all white spaces and
newline characters from a file.
Code:
#include <stdio.h>
#include <stdlib.h>
void main(){
FILE *fp,*fp1;
char ch;
fp=fopen("g.txt","r");
fp1=fopen("g2.txt","w");
if(fp==NULL) {
printf("Some problem in opening the file");
exit(0);
}
else {
while((ch=fgetc(fp))!=EOF) {
if(ch==' '|| ch=='\n') {
}
else {
fprintf(fp1,"%c",ch);
}
}
}
fclose(fp);
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 41
C Programming Laboratory [13MCA16]
b. Find whether a given word exists in the file. If it exists
display the location of the word
Code:
#include <stdio.h>
#include<conio.h>
#include<string.h>
#include <stdlib.h>
void main(){
FILE *fp;
int line_num = 1;
int find_result = 0;
char temp[512];
char str[10];
printf("Enter a String");
scanf("%s",str);
fp=fopen("g.txt","r");
while(fgets(temp, 512, fp) != NULL) {
if((strstr(temp, str)) != NULL) {
printf("A match found on line: %d\n", line_num);
printf("\n%s\n", temp);
find_result++;
}
line_num++;
}
if(find_result == 0) {
printf("\nSorry, couldn't find a match.\n");
Prof. D.Samanta, Email: [email protected]
Page 42
C Programming Laboratory [13MCA16]
}
if(fp) {
fclose(fp);
}
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 43
C Programming Laboratory [13MCA16]
14. Write a C program to copy one file content to another
file without using inbuilt functions.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
FILE *fp1,*fp2;
char ch,fname1[20],fname2[20];
printf("\nEnter source file name :");
gets(fname1);
printf("\nEnter dest file name :");
gets(fname2);
fp1=fopen(fname1,"r");
fp2=fopen(fname2,"w");
if(fp1==NULL||fp2==NULL)
{
printf("unable to open");
exit(0);
}
do
{
ch=fgetc(fp1);
fputc(ch,fp2);
}
while(ch!=EOF);
fcloseall();
printf("\n Files copied Successfully");
Prof. D.Samanta, Email: [email protected]
Page 44
C Programming Laboratory [13MCA16]
getch();
}
Prof. D.Samanta, Email: [email protected]
Page 45