Program in C to Replace Capital C with Capital S in a File

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:

  1. Open the input file in read mode.
  2. Create an output file in write mode.
  3. Read the input file character by character.
  4. If the current character is ‘C’, write ‘S’ to the output file; otherwise, write the current character as is.
  5. Repeat steps 3-4 until the end of the file is reached.
  6. 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)

Q: What is the purpose of this program?

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.

Q: How do I specify the input file to be processed?

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.

Q: Will this program modify the original input file?

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”.

Q: Can I replace characters other than “C” with this program?

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.

Q: Is it possible to perform case-insensitive replacements?

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'.

Q: How can I handle errors when opening or reading files?

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