Challenge: 
Winner?: 
No
Code Snippet: 
Dim Quotes(100) as String

path$=cl$()
if path$>""

n=len(path$)
filename$=left$(right$(path$,n-1),n-2)

open to read 1,filename$
if file exist (filename$+"_IF_Then_Conv.dba") then delete file filename$+"_IF_Then_Conv.dba"
open to write 2,filename$+"_IF_Then_Conv.dba"

repeat
read string 1,line$
words$=IF_Then_Conv(line$)
write string 2,words$
until file end(1)=1

close file 1
close file 2

print "Code "IF Then" statements have been converted."

wait key
endif


Function IF_Then_Conv(text$)
   local if_pos as DWORD
   local thn_pos as DWORD
   local else_pos as DWORD
   local IfCount as Integer

   text$=Quote_Replace(text$)

   `Do a look through for IF's and THEN's.
   thn_pos=FIND SUB STRING$(lower$(text$)," then ")
   if thn_pos<1
      text$=Quote_Restore(text$)
      ExitFunction text$
   Endif
   if_pos=FIND SUB STRING$(lower$(text$),"if ")
   if if_pos<1
      text$=Quote_Restore(text$)
      ExitFunction text$
   Endif

   IfCount=0
   new_part1$=""
   Do
      thn_pos=FIND SUB STRING$(lower$(text$)," then ")
      if thn_pos<1 Then Exit

      if_pos=FIND SUB STRING$(lower$(text$),"if ")
      if if_pos<1 Then Exit

      if if_pos>thn_pos
         text$=Left$(text$,thn_pos-1)+" NEHT "+Right$(text$,len(text$)-(thn_pos+5))
      Else
         new_part1$=new_part1$ + Left$(text$,thn_pos-1)+":"
         text$=Right$(text$,len(text$)-(thn_pos+5))
         text$=Replace_Text(text$,": Else ",":Else ")
         text$=Replace_Text(text$," Else ",":Else ")
         Inc IfCount
      Endif
   Loop
   text$=new_part1$+text$
   For i = 1 to IfCount
      text$=text$+":Endif"
   Next i
   text$=Quote_Restore(text$)
Endfunction text$


Function Quote_Replace(text$)
   `Temporarily replace Quotes so we don't confuse them in the search strings
   place=0:qn=0
   Do
      place=place+1
      if place > len(text$) Then Exit
      l$=Mid$(text$,place)
      if l$=Chr$(34)
         t$=l$
         Do
            place=place+1
            l$=Mid$(text$,place)
            t$=t$+l$
            if l$=Chr$(34) Then Exit
         Loop
         Quotes(qn)=t$
         text$=Replace_Text(text$,t$,"QQuote"+Str$(qn))
         Place=0
         qn=qn+1
      Endif
   Loop
Endfunction text$

Function Quote_Restore(text$)
   `Replaces the quotes from a previous Quote Replace
   qn=0
   Do
      If Quotes(qn)="" Then Exit
      text$=Replace_Text(text$,"QQuote"+Str$(qn),Quotes(qn))
      Quotes(qn)=""
      qn=qn+1
   Loop
Endfunction text$


Function Replace_Text(text$,find$,replace$)
   local find_pos as DWORD
   find_pos=FIND SUB STRING$(lower$(text$),lower$(find$))
   if find_pos<1 Then ExitFunction text$
   new_text$=Left$(text$,find_pos-1)+replace$+Right$(text$,len(text$)-(find_pos+(len(find$)-1)))
Endfunction new_text$