How can I do this?
BlitzPlus Forums/BlitzPlus Beginners Area/How can I do this?
| ||
| So.. Basically, I want to make this program so that I can type a sentence or string of text and it replaces each letter with a different letter (which I will specify). For example, let's say I want to replace all T's with F's. Input: Tootsie Rolls are a tremendous treat for toddlers! Output: Foofsie Rolls are a fremendous freaf for foddlers! All I have right now is the basics of the program (the asking for input, etc.), so it wouldn't really make a difference to post my code. Thanks for any help! |
| ||
| Well... just use REPLACE command to .. replace letters in a string? http://www.blitzbasic.com/bpdocs/command.php?name=replace&ref=goto |
| ||
Thanks! Now I have another problem. It was horribly messing up when I decrypted, then I realized that it was going in order. So like, if I changed all s's into d's then changed all d's into h's then all s's were h's, so reverting it back to the original string became impossible using the same algorithm..
string2enc$ = Input("Please type the text you'd like to encrypt: ")
Print ""
encstring$ = Replace$(string2enc$,"b","i")
encstring$ = Replace$(encstring$,"n","f")
encstring$ = Replace$(encstring$,"g","w")
encstring$ = Replace$(encstring$,"r","n")
encstring$ = Replace$(encstring$,"w","h")
encstring$ = Replace$(encstring$,"o","j")
encstring$ = Replace$(encstring$,"e","x")
encstring$ = Replace$(encstring$,"u","l")
encstring$ = Replace$(encstring$,"j","g")
encstring$ = Replace$(encstring$,"y","e")
encstring$ = Replace$(encstring$,"f","m")
encstring$ = Replace$(encstring$,"a","u")
encstring$ = Replace$(encstring$,"h","o")
encstring$ = Replace$(encstring$,"z","k")
encstring$ = Replace$(encstring$,"i","c")
encstring$ = Replace$(encstring$,"v","q")
encstring$ = Replace$(encstring$,"x","d")
encstring$ = Replace$(encstring$,"d","v")
encstring$ = Replace$(encstring$,"s","b")
encstring$ = Replace$(encstring$,"p","y")
encstring$ = Replace$(encstring$,"c","p")
encstring$ = Replace$(encstring$,"m","s")
encstring$ = Replace$(encstring$,"l","z")
encstring$ = Replace$(encstring$,"t","r")
encstring$ = Replace$(encstring$,"k","a")
encstring$ = Replace$(encstring$,"q","t")
Is there a way to.. perhaps, run all of the Replace's at once? Or is there some other method of making it so it doesn't replace already changed letters? |
| ||
| Ditch Replace() - it's not going to do it for you. Here's what I use (Blitzmax code, mind. Shouldn't be too hard to get it going in BlitzPlus). Const ENC_KEY:Int = %01101010101011111101000101011101 Local s:String = "Hello World" s = encryptString(s, ENC_KEY) 'encrypt it Print s s = encryptString(s, ENC_KEY) 'put it back Print s Function encryptString:String(txt:String, key:Int) If txt.Length > 0 Local out:String For Local n:Int = 1 To txt.Length out:+Chr(Asc(Mid(txt, n, 1)) ~ key) Next Return out EndIf End Function |
| ||
| @GfK Thanks for the response. I think I understand the logic behind the code you put, but I don't know how to actually use it.. It's been a long, long time since I used blitz, and I've never used anything but the more basic things.. (To sum up that paragraph, I have no idea what to do with the code you posted.) |
| ||
Hmm... this might run. Don't have Blitzplus so I can't check. I'm assuming you can use Const and Local. If not just replace both with Global.Const ENC_KEY% = %01101010101011111101000101011101 Local s$ = "Hello World" s = encryptString(s, ENC_KEY) 'encrypt it Print s s = encryptString(s, ENC_KEY) 'put it back Print s Function encryptString$(txt$, key%) If Len(txt) > 0 Local out$ For Local n% = 1 To Len(txt) out = out + Chr(Asc(Mid(txt, n, 1)) Xor key) Next Return out EndIf End Function |
| ||
| It would appear that it's not having any problem with Local and Const, but it says "Expecting End-Of-File" |
| ||
Wondering if you can't define local vars within a For/Next loop in BP.Const ENC_KEY% = %01101010101011111101000101011101 Local s$ = "Hello World" s = encryptString(s, ENC_KEY) 'encrypt it Print s s = encryptString(s, ENC_KEY) 'put it back Print s Function encryptString$(txt$, key%) If Len(txt) > 0 Local out$ For n% = 1 To Len(txt) out = out + Chr(Asc(Mid(txt, n, 1)) Xor key) Next Return out EndIf End Function If that doesn't work, somebody else with Blitzplus experience will have to fix it up. I'm sure it's fairly close, though. |
| ||
I ran this one again:Const ENC_KEY% = %01101010101011111101000101011101 Local s$ = "Hello World" s = encryptString(s, ENC_KEY) 'encrypt it Print s s = encryptString(s, ENC_KEY) 'put it back Print s Function encryptString$(txt$, key%) If Len(txt) > 0 Local out$ For Local n% = 1 To Len(txt) out = out + Chr(Asc(Mid(txt, n, 1)) Xor key) Next Return out EndIf End Function And I got it working how I like it. I wasn't really going for it to do a bunch of crazy symbols, but I still don't REALLY understand this much at all, so I don't know how I could possibly fix that. Besides that, it's great! Thanks for the help. If you could tell me how to manipulate the algorithm that'd be great. |
| ||
| The alternative way if you want specific letters to replace other letters like in your initial example is to have an array as a lookup for each character then iterate over the string and replace each character using array lookup one character at a time. |