/* 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 find_and_replace(char *s1, char *s2)
{
   int ix1,ix2,l;
   ix1 = 0;
   ix2 = 0;
   while(ix1 < strlen(s1))
   {
       if(strncmp(s1+ix1,"http://",7)==0)
       {
           s2[ix2] = '\0';
           strcat(s2,"<a href=\"");
           ix2 += 9;
           l = 0;
           while(s1[ix1+l]!=' ' && s1[ix1+l]!=',' && s1[ix1+l]!='\0') l++;
           strncat(s2,s1+ix1,l);
           ix2 += l;
           strcat(s2,"\" target=_blank>");
           ix2 += 16;
           strncat(s2,s1+ix1+7,l-7);
           ix2 += (l - 7);
           strcat(s2,"</a>");
           ix2 += 4;
           ix1 += l;
       }
       else
       {
           s2[ix2] = s1[ix1];
           ix1++;
           ix2++;
       }
   }
   s2[ix2] = '\0';
}

int main()
{
   char input[] = "Dette er en prøve http://www.eksperten.dk, hvor\n"
                  "vi tester om vi kan finde links som http://www.eksperten.dk !\n";
   char *output;
   printf("%s",input);
   output = (char *)malloc(4*strlen(input));
   find_and_replace(input,output);
   printf("%s",output);
   free(output);
   return 0;
}
