/* 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) */
//#define _UNICODE
//#define UNICODE
#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <tchar.h>

void replace(TCHAR *s,TCHAR *s1,TCHAR *s2)
{
  int i,j;
  TCHAR *tmp;
  tmp = (TCHAR *)malloc((_tcslen(s)+1)*sizeof(TCHAR));
  _tcscpy(tmp,s);
  i = 0;
  j = 0;
  while(i<_tcslen(tmp)) {
      if(_tcsncmp(tmp+i,s1,_tcslen(s1))==0) {
        _tcscpy(s+j,s2);
        i=i+_tcslen(s1);
        j=j+_tcslen(s2);
      } else {
        s[j]=tmp[i];
        i++;
        j++;
      }
  }
  s[j] = (TCHAR)'\0';
  free(tmp);
  return;
}

int main()
{
   TCHAR s[] = _T("test 1 test 2 test 3");
   printf("%d %d\n",sizeof(s),_tcslen(s));
   replace(s,_T("test"),_T(""));
   printf("%d\n",_tcslen(s));
   return 0;
}
