by Dinesh Thakur Category: C Programming (Pratical)

This program asks user to replace the occurrence of M by N. user declare variables and some method for replacement of occurrences. Like user ask to put file name here if condition will come in use for checking out the file exist or not if not the message will be “file can’t be open” else the while condition will use in for passing to “EOF”. If the condition match if(ch=='M'||ch=='m') then the process will use method to replace the letter with N from M. display result on the screen at last instance.

Problem Statement:

This is C program that ask user to replace the Letter M by N.

  1. Declare the variables.
  2. Using control statements.
  3. Display result on the screen.

Here is C source code to replace the Letter M by N. The output of this program shown below. 

#include<stdio.h>
void main()
{
     FILE *fp1;
     char ch,fname[20];
     printf("Enter file name=");
     scanf("%s",& fname);
     fp1=fopen(fname,"r+");
     if(fp1==NULL)
       {
            printf("File can't be opened");
            exit(0);
       }
            ch=fgetc(fp1);
            while(ch!=EOF)
                   {
                            if(ch=='M'||ch=='m')
                              {
                                    fseek(fp1,-1,1);
                                    fputc('N',fp1);
                              }
                                    fseek(fp1,0,1);
                                    ch=fgetc(fp1);
                  }
                                    fclose(fp1);
}
About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Search Content