Skip to content

Instantly share code, notes, and snippets.

@SirSaleh
Created March 27, 2015 15:09
Show Gist options
  • Save SirSaleh/b9756c8c38bf9ddda52a to your computer and use it in GitHub Desktop.
Save SirSaleh/b9756c8c38bf9ddda52a to your computer and use it in GitHub Desktop.
A code for Dear Sachida Barik To Exchange two word in a text file
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
//count_data();
void main()
{
// calling function
count_data();
getch();
}
count_data() // function for count no of words,lines & characters.
{
FILE *fp,*fp_rep;
char ch,ch1,temp_str[50],first_str[10],second_str[10];
int count=0; // counter
clrscr();
fp=fopen("c:\\a.txt","r");
fp_rep=fopen("c:\\out.txt","w");
printf("\nEnter String to find:");
scanf("%s",first_str);
printf("\nEnter String to replace:");
scanf("%s",second_str);
while((ch=getc(fp))!=EOF)
{
if(ch==' ')
{
temp_str[count]='\0';
if(strcmp(temp_str,first_str)==0)
{
fprintf(fp_rep," %s",second_str);
count=0;
}
else if (strcmp(temp_str,second_str)==0)
{
fprintf(fp_rep," %s",first_str);
count=0;
}
else
{
fprintf(fp_rep," %s",temp_str);
count=0;
}
}
else
{
temp_str[count++]=ch;
}
}
if(strcmp(temp_str,first_str)==0)
{
fprintf(fp_rep,"%s ",second_str);
count=0;
}
if(strcmp(temp_str,second_str)==0){
fprintf(fp_rep,"%s ",second_str);
count=0;
}
else
{
fprintf(fp_rep,"%s ",temp_str);
}
fclose(fp);
fclose(fp_rep);
remove("c:\\a.txt");
rename("c:\\out.txt","c:\\a.txt");
fflush(stdin);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment