0% found this document useful (0 votes)
6 views6 pages

Practical - File (FoC) (1) 28 To 31

The document contains multiple C programming experiments focusing on different concepts such as structures, arrays of structures, and file handling. Each experiment includes an aim, the concepts used, and a complete program with input/output examples. The experiments cover adding distances, complex numbers, storing employee information, and finding/replacing strings in files.
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)
6 views6 pages

Practical - File (FoC) (1) 28 To 31

The document contains multiple C programming experiments focusing on different concepts such as structures, arrays of structures, and file handling. Each experiment includes an aim, the concepts used, and a complete program with input/output examples. The experiments cover adding distances, complex numbers, storing employee information, and finding/replacing strings in files.
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/ 6

Experiment 28

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;

printf("Enter first distance (feet inch): ");


scanf("%d %f", &d1.feet, &d1.inch);
printf("Enter second distance (feet inch): ");
scanf("%d %f", &d2.feet, &d2.inch);

sum.feet = d1.feet + d2.feet;


sum.inch = d1.inch + d2.inch;

if (sum.inch >= 12.0)


{
sum.feet += (int)(sum.inch / 12);
sum.inch = (int)sum.inch % 12;
}

printf("Sum of distances: %d feet %.2f inches\n", sum.feet, sum.inch);


return 0;
}

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;

printf("Enter first complex number (real and imaginary parts): ");


scanf("%f %f", &c1.real, &c1.imag);

printf("Enter second complex number (real and imaginary parts): ");


scanf("%f %f", &c2.real, &c2.imag);

sum.real = c1.real + c2.real;


sum.imag = c1.imag + c2.imag;

printf("Sum of complex numbers: %.2f + %.2fi\n", sum.real, sum.imag);


return 0;
}

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);

printf("Enter skills ratings (5 values) for employee %d: \n", i + 1);


for(j = 0; j < 5; j++) {
scanf("%d", &employees[i].skills[j]);
}
printf("\n");
}
printf("\nEmployee Details:\n");
for(i = 0; i < 5; i++) {
printf("Employee %d\n", i + 1);
printf("ID: %d\n", employees[i].emp_id);
printf("Name: %s", employees[i].name);
printf("Salary: %.2f\n", employees[i].salary);
printf("Skills Ratings: ");
for(j = 0; j < 5; j++) {
printf("%d ", employees[i].skills[j]);
}
printf("\n\n");
}

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];

printf("Enter word to find: ");


scanf("%s", word);
printf("Enter word to replace with: ");
scanf("%s", replace);

fp1 = fopen("a.txt", "r");


fp2 = fopen("b.txt", "w");

while(fgets(line, sizeof(line), fp1)) {


char *ptr = line;
while((pos = strstr(ptr, word)) != NULL) {
count++;
int len = pos - ptr;
strncpy(buffer, ptr, len);
buffer[len] = '\0';
strcat(buffer, replace);
strcat(buffer, pos + strlen(word));
strcpy(ptr, buffer);
}
fputs(line, fp2);
}

fclose(fp1);
fclose(fp2);
remove("a.txt");
rename("b.txt", "a.txt");

printf("Total occurrences replaced: %d\n", count);


return 0;
}

Output:

You might also like