These exercises cover a range of dynamic memory allocation-related concepts and will help you practice and strengthen your skills in C programming. I have solved all of the above with explanatory comments. You should first make a sincere attempt at solving these problems before looking at the solutions.
Word Count:
#include <stdio.h>
int main() {
FILE *file;
char ch;
int wordCount = 0;
file = fopen("sample.txt", "r");
if (file == NULL) {
printf("Could not open the file.\n");
return 1;
}
while ((ch = fgetc(file)) != EOF) {
if (ch == ' ' || ch == '\n' || ch == '\t') {
wordCount++;
}
}
fclose(file);
printf("Word count: %d\n", wordCount);
return 0;
}
Character Frequency:
#include <stdio.h>
int main() {
FILE *inputFile, *outputFile;
char ch;
int frequency[256] = {0};
inputFile = fopen("input.txt", "r");
if (inputFile == NULL) {
printf("Could not open the input file.\n");
return 1;
}
while ((ch = fgetc(inputFile)) != EOF) {
frequency[ch]++;
}
fclose(inputFile);
outputFile = fopen("output.txt", "w");
if (outputFile == NULL) {
printf("Could not open the output file.\n");
return 1;
}
for (int i = 0; i < 256; i++) {
if (frequency[i] > 0) {
fprintf(outputFile, "Character '%c' occurs %d times.\n", i, frequency[i]);
}
}
fclose(outputFile);
return 0;
}
File Copy:
#include <stdio.h>
int main() {
FILE *sourceFile, *destinationFile;
char ch;
sourceFile = fopen("source.txt", "r");
if (sourceFile == NULL) {
printf("Could not open the source file.\n");
return 1;
}
destinationFile = fopen("destination.txt", "w");
if (destinationFile == NULL) {
printf("Could not open the destination file.\n");
fclose(sourceFile);
return 1;
}
while ((ch = fgetc(sourceFile)) != EOF) {
fputc(ch, destinationFile);
}
fclose(sourceFile);
fclose(destinationFile);
return 0;
}
Line Numbers:
#include <stdio.h>
int main() {
FILE *inputFile, *outputFile;
char line[100];
int lineNumber = 1;
inputFile = fopen("input.txt", "r");
if (inputFile == NULL) {
printf("Could not open the input file.\n");
return 1;
}
outputFile = fopen("output.txt", "w");
if (outputFile == NULL) {
printf("Could not open the output file.\n");
fclose(inputFile);
return 1;
}
while (fgets(line, sizeof(line), inputFile) != NULL) {
fprintf(outputFile, "%d: %s", lineNumber, line);
lineNumber++;
}
fclose(inputFile);
fclose(outputFile);
return 0;
}
File Encryption:
#include <stdio.h>
int main() {
FILE *inputFile, *outputFile;
char ch;
inputFile = fopen("input.txt", "r");
if (inputFile == NULL) {
printf("Could not open the input file.\n");
return 1;
}
outputFile = fopen("output.txt", "w");
if (outputFile == NULL) {
printf("Could not open the output file.\n");
fclose(inputFile);
return 1;
}
while ((ch = fgetc(inputFile)) != EOF) {
fputc(ch + 1, outputFile);
}
fclose(inputFile);
fclose(outputFile);
return 0;
}
File Concatenation:
#include <stdio.h>
int main() {
FILE *file1, *file2, *outputFile;
char ch;
file1 = fopen("file1.txt", "r");
if (file1 == NULL) {
printf("Could not open file1.\n");
return 1;
}
file2 = fopen("file2.txt", "r");
if (file2 == NULL) {
printf("Could not open file2.\n");
fclose(file1);
return 1;
}
outputFile = fopen("output.txt", "w");
if (outputFile == NULL) {
printf("Could not open the output file.\n");
fclose(file1);
fclose(file2);
return 1;
}
while ((ch = fgetc(file1)) != EOF) {
fputc(ch, outputFile);
}
while ((ch = fgetc(file2)) != EOF) {
fputc(ch, outputFile);
}
fclose(file1);
fclose(file2);
fclose(outputFile);
return 0;
}
Record Management:
#include <stdio.h>
#include <stdlib.h>
struct Record {
int id;
char name[50];
float salary;
};
void displayMenu() {
printf("\nRecord Management System\n");
printf("1. Add Record\n");
printf("2. Delete Record\n");
printf("3. Search Record\n");
printf("4. Modify Record\n");
printf("5. Display All Records\n");
printf("6. Exit\n");
printf("Enter your choice: ");
}
void addRecord(FILE *file) {
struct Record record;
printf("Enter ID: ");
scanf("%d", &record.id);
printf("Enter name: ");
scanf("%s", record.name);
printf("Enter salary: ");
scanf("%f", &record.salary);
fseek(file, 0, SEEK_END);
fwrite(&record, sizeof(struct Record), 1, file);
printf("Record added successfully.\n");
}
void deleteRecord(FILE *file) {
int targetID;
printf("Enter the ID to delete: ");
scanf("%d", &targetID);
FILE *tempFile = fopen("temp.dat", "wb");
struct Record record;
while (fread(&record, sizeof(struct Record), 1, file) == 1) {
if (record.id != targetID) {
fwrite(&record, sizeof(struct Record), 1, tempFile);
}
}
fclose(file);
fclose(tempFile);
remove("records.dat");
rename("temp.dat", "records.dat");
printf("Record deleted successfully.\n");
}
void searchRecord(FILE *file) {
int targetID;
printf("Enter the ID to search: ");
scanf("%d", &targetID);
struct Record record;
fseek(file, 0, SEEK_SET);
while (fread(&record, sizeof(struct Record), 1, file) == 1) {
if (record.id == targetID) {
printf("Record found:\n");
printf("ID: %d, Name: %s, Salary: %.2f\n", record.id, record.name, record.salary);
return;
}
}
printf("Record with ID %d not found.\n", targetID);
}
void modifyRecord(FILE *file) {
int targetID;
printf("Enter the ID to modify: ");
scanf("%d", &targetID);
struct Record record;
fseek(file, 0, SEEK_SET);
while (fread(&record, sizeof(struct Record), 1, file) == 1) {
if (record.id == targetID) {
printf("Enter new name: ");
scanf("%s", record.name);
printf("Enter new salary: ");
scanf("%f", &record.salary);
fseek(file, -sizeof(struct Record), SEEK_CUR);
fwrite(&record, sizeof(struct Record), 1, file);
printf("Record modified successfully.\n");
return;
}
}
printf("Record with ID %d not found.\n", targetID);
}
void displayAllRecords(FILE *file) {
struct Record record;
fseek(file, 0, SEEK_SET);
while (fread(&record, sizeof(struct Record), 1, file) == 1) {
printf("ID: %d, Name: %s, Salary: %.2f\n", record.id, record.name, record.salary);
}
}
int main() {
FILE *file = fopen("records.dat", "ab+");
if (file == NULL) {
printf("Could not open the file.\n");
return 1;
}
int choice;
do {
displayMenu();
scanf("%d", &choice);
switch (choice) {
case 1:
addRecord(file);
break;
case 2:
deleteRecord(file);
break;
case 3:
searchRecord(file);
break;
case 4:
modifyRecord(file);
break;
case 5:
displayAllRecords(file);
break;
case 6:
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 6);
fclose(file);
return 0;
}
File Search:
#include <stdio.h>
#include <string.h>
int main() {
FILE *file;
char line[100], searchWord[20];
int lineNumber = 1;
file = fopen("textfile.txt", "r");
if (file == NULL) {
printf("Could not open the file.\n");
return 1;
}
printf("Enter the word to search: ");
scanf("%s", searchWord);
while (fgets(line, sizeof(line), file) != NULL) {
if (strstr(line, searchWord) != NULL) {
printf("Word found at line %d: %s", lineNumber, line);
}
lineNumber++;
}
fclose(file);
return 0;
}
CSV File Handling:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Employee {
int id;
char name[50];
float salary;
};
int readCSVFile(const char *filename, struct Employee **employees) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open the file.\n");
return -1;
}
int capacity = 10;
int size = 0;
*employees = malloc(capacity * sizeof(struct Employee));
if (*employees == NULL) {
printf("Memory allocation failed.\n");
fclose(file);
return -1;
}
while (fscanf(file, "%d,%49[^,],%f\n", &(*employees)[size].id, (*employees)[size].name, &(*employees)[size].salary) == 3) {
size++;
if (size >= capacity) {
capacity *= 2;
*employees = realloc(*employees, capacity * sizeof(struct Employee));
if (*employees == NULL) {
printf("Memory reallocation failed.\n");
fclose(file);
return -1;
}
}
}
fclose(file);
return size;
}
void printRecords(struct Employee *employees, int size) {
printf("ID\tName\t\tSalary\n");
for (int i = 0; i < size; i++) {
printf("%d\t%-15s\t%.2f\n", employees[i].id, employees[i].name, employees[i].salary);
}
}
int compareByID(const void *a, const void *b) {
return ((struct Employee *)a)->id - ((struct Employee *)b)->id;
}
void filterBySalary(struct Employee *employees, int size, float threshold) {
printf("\nEmployees with Salary above %.2f:\n", threshold);
printf("ID\tName\t\tSalary\n");
for (int i = 0; i < size; i++) {
if (employees[i].salary > threshold) {
printf("%d\t%-15s\t%.2f\n", employees[i].id, employees[i].name, employees[i].salary);
}
}
}
int main() {
const char *filename = "employees.csv";
struct Employee *employees;
int size;
size = readCSVFile(filename, &employees);
if (size == -1) {
return 1;
}
printf("Original Records:\n");
printRecords(employees, size);
qsort(employees, size, sizeof(struct Employee), compareByID);
printf("\nRecords after sorting by ID:\n");
printRecords(employees, size);
filterBySalary(employees, size, 50000);
free(employees);
return 0;
}
File Deletion:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *file, *tempFile;
char line[100];
int lineToDelete;
file = fopen("data.txt", "r");
if (file == NULL) {
printf("Could not open the file.\n");
return 1;
}
tempFile = fopen("temp.txt", "w");
if (tempFile == NULL) {
printf("Could not open the temporary file.\n");
fclose(file);
return 1;
}
printf("Enter the line number to delete: ");
scanf("%d", &lineToDelete);
int currentLine = 1;
while (fgets(line, sizeof(line), file) != NULL) {
if (currentLine != lineToDelete) {
fprintf(tempFile, "%s", line);
}
currentLine++;
}
fclose(file);
fclose(tempFile);
remove("data.txt");
rename("temp.txt", "data.txt");
return 0;
}