Introduction
In this article, we will explore how to write a program in the C programming language to replace all occurrences of the capital letter “C” with the capital letter “S” in a file.
In the realm of programming, the ability to manipulate and modify files is an essential skill.
Also Read: Leap Year Program in C: Simplifying the Logic
One common task is replacing specific characters within a file with other characters.
This program can be a valuable tool when working with textual data and needing to make such replacements efficiently.
Program in C to Replace Capital C with Capital S in a File
To begin, let’s take a look at the program’s structure and the steps involved in replacing the capital “C” with the capital “S” in a file:
- Open the input file in read mode.
- Create an output file in write mode.
- Read the input file character by character.
- If the current character is ‘C’, write ‘S’ to the output file; otherwise, write the current character as is.
- Repeat steps 3-4 until the end of the file is reached.
- Close both the input and output files.
Also Read: C Program to Find the Inverse of 2×2 Matrix
Here’s the C program implementation for achieving the aforementioned functionality:
#include <stdio.h>
int main() {
FILE *inputFile, *outputFile;
char ch;
inputFile = fopen("input.txt", "r");
outputFile = fopen("output.txt", "w");
if (inputFile == NULL || outputFile == NULL) {
printf("Error opening files.");
return 1;
}
while ((ch = fgetc(inputFile)) != EOF) {
if (ch == 'C')
fputc('S', outputFile);
else
fputc(ch, outputFile);
}
fclose(inputFile);
fclose(outputFile);
printf("Replacement completed successfully.");
return 0;
}
Save the above code in a file named replace.c
and compile it using a C compiler.
Execute the resulting executable, and the program will replace all occurrences of the capital letter “C” with the capital letter “S” in the input.txt
file, creating a new file named output.txt
.
Also Read: C Program to Add Alternate Elements of a 2D Array
FAQs (Frequently Asked Questions)
The purpose of this program is to replace all instances of the capital letter “C” with the capital letter “S” in a given file. It provides a convenient and efficient way to make such replacements programmatically.
The program assumes that the input file is named input.txt
and is located in the same directory as the program itself. If your input file has a different name or location, you can modify the program accordingly by changing the file name in the fopen
function call.
No, this program will not modify the original input file. Instead, it will create a new file named output.txt
where all occurrences of the capital letter “C” are replaced with the capital letter “S”.
Yes, you can modify the program to replace characters other than “C” by modifying the condition in the if
statement. Simply change the character comparison from ch == 'C'
to the desired character you wish to replace.
Yes, it is possible to perform case-insensitive replacements by adding an additional condition to the if
statement. For example, you can replace both “C” and “c” with “S” by modifying the condition to ch == 'C' || ch == 'c'
.
The program includes error checking to ensure that the input and output files are successfully opened. If there is an error, such as a file not found or a permission issue, the program will display an error message. You can handle these errors by checking the return values of the fopen
function calls and taking appropriate action.
Also Read: C Program to Copy the Contents of One File into Another File
Conclusion
In conclusion, the ability to replace specific characters within a file using a program can be a valuable skill for any programmer.
The program in the C programming language provided in this article demonstrates how to replace all occurrences of the capital letter “C” with the capital letter “S” in a file.
Also Read: Getchar and Putchar Function in C with Example
By following the outlined steps and understanding the code, you can modify the program to suit your specific requirements.
Use this knowledge to efficiently manipulate and modify textual data, saving time and effort in the process.
Also Read: Best 5 Programs on Fibonacci Series in C