/*C Linux Code to count repeated word and replace it with another word*/
#include<stdio.h>
#include<stdlib.h>
void count_data();
int main()
{
// calling function
count_data();
return 0;
}
void count_data() // function for count no of words,lines & characters.
{
FILE *fp,*fp_rep;
char ch,ch1,temp_str[50],rep_str[10],new_str[10];
int count=0; // counter
fp=fopen("m1.txt","r");
fp_rep=fopen("m2.txt","w");
printf("\nEnter String to find:");
scanf("%s",rep_str);
printf("\nEnter String to replace:");
scanf("%s",new_str);
int wordcount=0;
while((ch=getc(fp))!=EOF)
{
if(ch==' ')
{
temp_str[count]='\0';
if(strcmp(temp_str,rep_str)==0)
{
wordcount++;
printf("Replacing):");
//ch1=getchar();
//if(ch1=='y')
//{
fprintf(fp_rep," %s",new_str);
count=0;
// }
// else
//{ fprintf(fp_rep," %s",temp_str);count=0;}
}
else
{
fprintf(fp_rep," %s",temp_str);
count=0;
}
}else
{
temp_str[count++]=ch;
}
}
printf("Total number of %s repeated=%d\n",rep_str,wordcount);
/* if(strcmp(temp_str,rep_str)==0)
{
printf("Do u want to replace(y/n):");
ch1=getchar();
if(ch1=='y')
{
fprintf(fp_rep,"%s ",new_str);
count=0;
}
else
{
fprintf(fp_rep,"%s ",temp_str);
count=0;
}
}else
{
fprintf(fp_rep,"%s ",temp_str);
}*/
fclose(fp);
fclose(fp_rep);
//remove("m1.txt");
//rename("m2.txt","m1.txt");
fflush(stdin);
}
#include<stdio.h>
#include<stdlib.h>
void count_data();
int main()
{
// calling function
count_data();
return 0;
}
void count_data() // function for count no of words,lines & characters.
{
FILE *fp,*fp_rep;
char ch,ch1,temp_str[50],rep_str[10],new_str[10];
int count=0; // counter
fp=fopen("m1.txt","r");
fp_rep=fopen("m2.txt","w");
printf("\nEnter String to find:");
scanf("%s",rep_str);
printf("\nEnter String to replace:");
scanf("%s",new_str);
int wordcount=0;
while((ch=getc(fp))!=EOF)
{
if(ch==' ')
{
temp_str[count]='\0';
if(strcmp(temp_str,rep_str)==0)
{
wordcount++;
printf("Replacing):");
//ch1=getchar();
//if(ch1=='y')
//{
fprintf(fp_rep," %s",new_str);
count=0;
// }
// else
//{ fprintf(fp_rep," %s",temp_str);count=0;}
}
else
{
fprintf(fp_rep," %s",temp_str);
count=0;
}
}else
{
temp_str[count++]=ch;
}
}
printf("Total number of %s repeated=%d\n",rep_str,wordcount);
/* if(strcmp(temp_str,rep_str)==0)
{
printf("Do u want to replace(y/n):");
ch1=getchar();
if(ch1=='y')
{
fprintf(fp_rep,"%s ",new_str);
count=0;
}
else
{
fprintf(fp_rep,"%s ",temp_str);
count=0;
}
}else
{
fprintf(fp_rep,"%s ",temp_str);
}*/
fclose(fp);
fclose(fp_rep);
//remove("m1.txt");
//rename("m2.txt","m1.txt");
fflush(stdin);
}
No comments:
Post a Comment