Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C by OOP ( 8 years ago )
#include <stdio>
#include <string>
#define MAX 256

int main()
{
 FILE *file, *ftemp; 
 char filename[MAX], words[MAX];
 char temp[] = "temp.txt", str[MAX], *ptr, *tmp;

 /*get input */ 
 printf("Enter file name: ");
 scanf("%s", filename);

 /*read array of words*/
 printf("Enter words: ");
 int nr = 1;
 char buffer[128];
 int result = scanf("7[^\n]", buffer);
 if (result > 0)
 {
  char** array;
  for (int i = 0; buffer[i] != '\0'; i++)
  {
   if (buffer[i] == ' ' || buffer[i] == '\n' || buffer[i] == '\t')
   nr++;
  }
  array = malloc(nr * sizeof(char*));
  array[0] = strtok(buffer, " ");
  for(int w = 1; w < nr; w++)
   array[w] = strtok(NULL, " ");
 } 

 /*open input file in read mode*/
 file = fopen&#40;filename, "r"&#41;;
 ftemp = fopen&#40;temp, "w"&#41;;

 while(!feof(fp1))
 {
  strcpy(str, "\0");
  /*read line by line from the input file*/
  fgets(str, MAX, fp1);
  
  for (int i = 0; i < nr; i++)
  {

   if(strstr(str, array[i])
   {
    tmp = str;
    while(ptr = strstr(tmp, array[i])
    {
     /*letters befoore the word to delete*/
     while (tmp != ptr)
     {
      fputc(*tmp, ftemp);
      tmp++;
     }
     /*skip the word to delete*/
     ptr = ptr + strlen(array[i]);
     tmp = ptr;
    }
    while (*tmp != '\0')
    {
     fputc(*tmp, ftemp);
     tmp++;
    }
   }
   
  }

 }
 fclose(file);
 fcloe(ftemp);
 remove(file);
 rename(temp, filename);
 
 return 0;

 
}

 

Revise this Paste

Your Name: Code Language: