/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void replace(char *s,char *s1,char *s2)
{
  int i,j;
  char *tmp;
  tmp = (char *)malloc(strlen(s)+1);
  strcpy(tmp,s);
  i = 0;
  j = 0;
  while(i<strlen(tmp)) {
      if(strncmp(tmp+i,s1,strlen(s1))==0) {
        strcpy(s+j,s2);
        i=i+strlen(s1);
        j=j+strlen(s2);
      } else {
        s[j]=tmp[i];
        i++;
        j++;
      }
  }
  s[j] = '\0';
  free(tmp);
  return;
}

int main()
{
   char line[133];
   FILE *fp1,*fp2;
   fp1 = fopen("input.dat", "r");
   fp2 = fopen("output.dat", "w");
   while(!feof(fp1))
   {
      if(fgets(line,sizeof(line),fp1))
      {
          replace(line,"hund","kat");
          fputs(line,fp2);
      }
   }
   fclose(fp1);
   fclose(fp2);
   return 0;
}
