My issue
I tried to solve this question but didn’t get right approach and solution is showing wrong answer in console , so pls tell correct solution.
My code
void userFunction() {
FILE *file = fopen("/mnt/codechef/input.txt", "r+"); // Open the "input.txt" file for both reading and writing
if (file == NULL) {
perror("Error opening file"); // Print an error message if file opening fails
return 1; // Return an error code
}
char buffer[100]; // Buffer to store each line
// Read lines from the file, find and replace 'Codechef'
while (fgets(buffer, sizeof(buffer), file) != NULL) { // Read lines from the file
char *found = strstr(buffer, "Codechef"); // Search for 'Codechef' in the line
while (found != NULL) { // Continue searching within the line
fseek(file, found - buffer, SEEK_CUR); // Move the file pointer to the start of the match
fputs("CodeChef", file); // Write 'CodeChef' over 'Codechef'
fseek(file, strlen("CodeChef") - strlen("Codechef"), SEEK_CUR);
// Move the file pointer to maintain alignment with the rest of the content
found = strstr(found + strlen("Codechef"), "Codechef"); // Search for the next occurrence
}
}
fclose(file); // Close the file
system("cat /mnt/codechef/input.txt");
}
Learning course: Programming using C
Problem Link: CodeChef: Practical coding for everyone