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

Introduction

This is a very important program written in c. In this c program, we are using the concept of file handling. Because we are performing two operations on the file, i.e. reading a file and writing to the file.

Also Read: C Language Program to Count the Number of Lowercase Letters in a Text File

In this c program, we must read the contents of a file one character at a time. Because we have to check whether the character is capital c or not. If it is capital c, then we will replace it with capital s and write it into another file. But if we find that the character is not capital c, then we will not replace it with capital s, but we will write it into another file.

Input File: abc.txt

Hello, C programmers, how are you? I know you are a champion in C programming. C is always close to my heart. Now, we will write a program in c to display the simple message “Welcome to C programming!”.

The above file is our input file. Suppose that the file name is “abc.txt”. We are reading this file and after the execution of our c program, the contents of this file will be changed.

Output File: abc.txt

Hello, S programmers, how are you? I know you are a champion in S programming. S is always close to my heart. Now, we will write a program in c to display the simple message “Welcome to S programming!”.

Also Read: C Program to Remove White Spaces and Comments from a File

Now, you can observe the contents of the output file. In this file, capital c is replaced with capital s.

Now, let’s see our c program.

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

#include <stdio.h>
#include <stdlib.h>
int main()
{
    FILE *fp1,*fp2;
    char ch;
    fp1=fopen("abc.txt","r");
    fp2=fopen("temp.txt","w");
    if(fp1==NULL)
    {
        printf("Error while opening a file for reading");
        return 0;
    }
    if(fp2==NULL)
    {
        printf("Error while opening a file for writing");
        return 0;
    }
    printf("Before Replacing capital Cn");
    while((ch=fgetc(fp1))!=EOF)
    {
        printf("%c",ch);
        if(ch=='C')
        {
            fputc('S',fp2);
        }
        else
        {
            fputc(ch,fp2);
        }
    }
    fclose(fp1);
    fclose(fp2);
    remove("abc.txt");
    rename("temp.txt","abc.txt");
    fp1=fopen("abc.txt","r");
    if(fp1==NULL)
    {
        printf("Error while opening for reading");
        return 0;
    }
    printf("\nAfter Replacing capital C with S\n");
    while((ch=fgetc(fp1))!=EOF)
    {
       printf("%c",ch);
    }
    fclose(fp1);
    return 0;
}

Detailed Explanation of this C Program:

FILE *fp1,*fp2;

We are dealing here with two files i.e. one for reading and one for writing. But for dealing with files, we need to first create file pointers of FILE structure.

So, here we are declaring two file pointers i.e. *fp1 and *fp2. These file pointers will link to the files that we want to deal with.

Also Read: C Program to Copy the Contents of One File into Another File

char ch;

We know, we are writing a program in c to read and write a character from/to the files. This is our basic operation. So, when we read a character from a file, we need one variable in our program to store. A similar variable will be used to write in a file. That is why we have declared variable ch of type char.

fp1=fopen(“abc.txt”,”r”);

This function fopen() open a file i.e. abc.txt and “r” means in reading. When we want to open a file, we have to specify the mode of operation i.e. reading or writing. So, we are opening the abc.txt file in reading mode.

fp2=fopen(“temp.txt”,”w”);

Similarly, we are opening here another file using the fopen() function. That file is temp.txt and we are opening that file for writing purposes. This is a temporary file.

Also Read: Interview Questions On C

Initially, we will write our output in this file. After finishing our operation i.e. reading from a file abc.txt, we will delete this file. Why? Because our output is stored in temp.txt file. Now, after deleting the abc.txt file, we will use rename() function to rename the temp.txt file with abc.txt. So this is the logic behind this.

if(fp1==NULL)

We know, fp1 is the file pointer. When there is some problem with opening a file, then fopen() returns a NULL value and that value is stored in fp1. In this, if statement we are checking whether the value of fp1 is equal to NULL or not. If it is true, then we will display the error message and terminate the program, otherwise this body of if will not be executed.

printf(“Error while opening a file for reading”);

As I talked about above, if the value of fp1 is equal to NULL, then this printf() will display this error message that Error while opening a file for reading.

if(fp2==NULL) 

This has a similar explanation like if(fp1==NULL). The only difference is, there was fp1 and here is fp2.

printf(“Before Replacing capital C\n”);

We know printf() display output on the console. So, here we are just displaying the simple message before updating a file. This means, here we are displaying the original contents of the abc.txt file.

while((ch=fgetc(fp1))!=EOF) 

This is a while loop statement. This will repeat its body until the condition is true. Which condition? In this while loop, we are evaluating two expressions:

Also Read: Switch Case in C Program to Calculate Area of Circle and Triangle

ch=fgetc(fp1)

The fgetc() function reads a single character from a file that is associated with fp1. We know, fp1 is a file pointer that points to the abc.txt file. In this statement, fgetc() is reading a single character from abc.txt and storing that character in the variable ch. After storing that character in the variable ch, now we are comparing that value with EOF i.e. End of the File. How?

ch!=EOF

I have split a single statement into two statements. So after reading the character and storing it in the variable ch, we will compare it with the EOF. If ch and EOF are not equal, then and only then, the body of the while loop will be executed. Otherwise, this loop will be terminated and control will transfer to the next statement followed by the while loop.

But this condition will be false when the complete file will be read.

printf(“%c”,ch);

Inside the while loop, the first statement is this printf() statement. This statement will display the value of ch to the console.

if(ch==’C’)

This is the actual statement that we are waiting for. This is if statement and there is one condition and if that condition becomes true, then the body of this if statements will be executed.

Also Read: Perfect Number in C Programming using All Loops

Here, we are checking, Is the value of ch is capital c? Because we are reading characters from a file and there may be the possibility of getting capital C. So we know, if we get capital c then write capital s to the temp.txt file instead of capital c.

fputc(‘S’,fp2);

This is the body of the above if statement. This statement will be executed only when the value of the ch is capital c.

So, fputc() is the file output function that writes a single character i.e. S into the file pointer fp2 which is associated with the temp.txt file.

fputc(ch,fp2);

But what if the value of ch is not equal to capital c? Then for this, we have written an else statement. So the above statement is written in the body of the else statement.

This fputc() function will write a character that is stored in the ch to the temp.txt file.

fclose() in c programming

After performing the read and write operation, we need to close the files. So for this purpose, we can use the fclose() function. The fclose(fp1) and fclose(fp2) closes the files abc.txt and temp.txt respectively.

remove(“abc.txt”); 

We can use this function remove() to delete a file from a directory. In this case, we are deleting the file abc.txt from the current directory. Why? Because, our output is stored in temp.txt file, not in abc.txt file.

rename(“temp.txt”,”abc.txt”);

This is also an important function in c programming. The rename() function renames the existing file with the new name. In this case, this function will rename the name of the temp.txt file to the abc.txt file. Now, if we check this file manually, then we can see our output.

Also Read: Reverse a Number in C

So, this was the program written in c to replace capital c with capital s. I hope you have enjoyed this explanation. I have tried to keep the language as simple as possible.

C Program to Replace Capital C with...
C Program to Replace Capital C with Capital S in a File | Files in C

Thank you for reading.

Some Important C Programs

  1. Program in C to Find Longest Line in a File
  2. Palindrome in C using Pointers
  3. Insert and Delete element in Array in C using switch case
  4. C Program to Add Alternate Elements of a 2D Array
  5. Arrays in C for Complete Beginners
  6. C Program to Find Area of a Circle using Preprocessor
  7. Program in C to Remove White Spaces and Comments from a File
  8. C Program to Print Numbers Except Multiples of n
  9. Reverse a Number using getchar and putchar function in c
  10. The while loop in C Programming

Leave a Comment