replacing parts of a string

Just starting out? Need help? Post your questions and find answers here.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by willinyork.

hi there. if for example I have an alphabet code (like you did when you were a child!) such as A (in the real alphabet) = E in your code, B=P, C=J, D=T etc - how do you do a replacestring() inorder to corretly "translate" one code to the other (or vice versa)? I do a normal placestring as above, but obviously it will replace them in sequence (so sometimes letters get changed twice, and the end string is incorrect). any advice is welcomed! thanks!
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Paul.

You could do something like this maybe?

Code: Select all

Global alpha.s,key.s
alpha=" ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  key=" QWERTYUIOPLKJHGFDSAZXCVBNM"
   
Procedure.s code(text.s)
  new.s=""
  For tmp=1 To Len(text)
    cur$=Mid(UCase(text),tmp,1)
    pos1=FindString(alpha,cur$,1)
    char$=Mid(key,pos1,1)
    new+char$
  Next
  ProcedureReturn new
EndProcedure
 
Procedure.s decode(text.s)
  new.s=""
  For tmp=1 To Len(text)
    cur$=Mid(UCase(text),tmp,1)
    pos1=FindString(key,cur$,1)
    char$=Mid(alpha,pos1,1)
    new+char$
  Next
  ProcedureReturn new
EndProcedure
 
 
codetext$=code("This is a test")
decodetext$=decode(codetext$)
 
MessageRequester("",codetext$+Chr(10)+decodetext$,0)
Post Reply