Practical - File (FoC) (1) 28 To 31
Practical - File (FoC) (1) 28 To 31
Aim: Write a C program to add two distances in inch & feet using the concept of structures.
Concept Used:
1. Structure: A structure is used to group related variables under one name. In this case, the structure
will hold the inches and feet of a distance.
2. Addition Logic:
• Add the feet values and inch values separately.
• If the total inches exceed 12 (as 1 foot = 12 inches), convert the extra inches into feet.
Program:
#include <stdio.h>
struct distance{
int feet;
float inch;
};
int main(){
struct distance d1, d2, sum;
Output:
Experiment 29
Aim: Write a C program to add two complex numbers using the concept of structures in C.
Concept Used:
1. Complex Numbers:
• A complex number has two components: real part and imaginary part.
• To add complex numbers, add their respective real parts and imaginary parts.
2. Structure: Use a structure to represent a complex number with two fields: real and
imaginary.
Program:
#include <stdio.h>
struct complex{
float real;
float imag;
};
int main(){
struct complex c1, c2, sum;
Output:
Experiment 30
Aim: Write a program in C to store the information of five employees using both concepts i.e. array
of structure and array within structure.
Concept Used:
1. Array of Structures:
• An array of structures allows storing multiple instances of a structure. For example, each
element of the array represents one employee.
• Used when multiple objects with the same data type need to be managed.
2. Array within Structure:
• A structure can contain arrays as its members. For example, an array to store marks or skills
within a structure representing an employee.
• Useful to represent additional details about each object as an array.
Program:
#include <stdio.h>
struct Employee {
int emp_id;
char name[50];
float salary;
int skills[5];
};
int main() {
struct Employee employees[5];
int i, j;
for(i = 0; i < 5; i++) {
printf("Enter details for employee %d\n", i + 1);
printf("Employee ID: ");
scanf("%d", &employees[i].emp_id);
getchar();
printf("Employee Name: ");
fgets(employees[i].name, sizeof(employees[i].name), stdin);
printf("Salary: ");
scanf("%f", &employees[i].salary);
return 0;
}
Output:
Experiment 31
Aim: Write a Program in C to find and replace a specific string in a file and also display the total
number of appearances of that string.
Concept Used:
• File Handling: fopen(), fgets(), fprintf(), fclose() for reading and writing files.
• String Functions: strstr(), strcpy(), strlen() for finding and replacing substrings.
• Temporary File Use: Write changes to a temp file, then replace the original.
• Counting Occurrences: Loop with strstr() to count matches.
• Line-by-Line Processing: Read and modify the file line by line.
• Pointer Manipulation: Used for locating and replacing substrings in lines.
Program:
#include <stdio.h>
#include <string.h>
int main() {
FILE *fp1, *fp2;
char line[1000], word[100], replace[100];
int count = 0;
char *pos, buffer[1000];
fclose(fp1);
fclose(fp2);
remove("a.txt");
rename("b.txt", "a.txt");
Output: