Jump to content

For...to...Next (StringReplace)


Adamrv1
 Share

Recommended Posts

My friend is asking me to create a translator for a language he made himself (its not so much a language as much as a manipulation of the english language). So far i have this:

#include <String.au3>

$String = InputBox("Translator", "Please enter a word: ")
$StringOrg = $String

Global $Org[26] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
Global $Rep[26] = ["e","c","c","p","a","f","b","y","i","r","zǝ","l","w","u","o","d","g","j","s","t","n","v","m","k","h","z"]

for $i = 0 to 25 step 1

    ;String Flip
    $String = StringReplace($String, $Org[$i], $Rep[$i])

Next

MsgBox(0,"Replacement String",  "Original: " & $StringOrg & @CR & @CR & "Replacement: " & $String)

That code should make a string(alphabet) become

eccpafbyirzǝlwuodgjstnvmkhz

however, the result comes out as:

accdafbhijzǝlmnodgjstnvmkhz

This shows that the following letters did not translate: a,d,h,j,m,n

I'm not sure why these don't translate but everything else does, please help.

Thank You

Link to comment
Share on other sites

My friend is asking me to create a translator for a language he made himself (its not so much a language as much as a manipulation of the english language). So far i have this:

#include <String.au3>

$String = InputBox("Translator", "Please enter a word: ")
$StringOrg = $String

Global $Org[26] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
Global $Rep[26] = ["e","c","c","p","a","f","b","y","i","r","zǝ","l","w","u","o","d","g","j","s","t","n","v","m","k","h","z"]

for $i = 0 to 25 step 1

    ;String Flip
    $String = StringReplace($String, $Org[$i], $Rep[$i])

Next

MsgBox(0,"Replacement String",  "Original: " & $StringOrg & @CR & @CR & "Replacement: " & $String)

That code should make a string(alphabet) become

eccpafbyirzǝlwuodgjstnvmkhz

however, the result comes out as:

accdafbhijzǝlmnodgjstnvmkhz

This shows that the following letters did not translate: a,d,h,j,m,n

I'm not sure why these don't translate but everything else does, please help.

Thank You

Can you please check the code carefully.Actually a,d,h,j,m,n are replaced with corresponding letters say e,p,y,r,w,u in during 1st iteration.But what happens in next iteration these are inturn replaced by a,d,h,j,m,n. That's why you are getting output like that.

[size="4"][font="Arial Narrow"][font="Garamond"]Attitude is a little thing that makes a big difference[/font][/font][/size][indent][/indent]

Link to comment
Share on other sites

Run this code from within SciTEand you might see what is happening:

$String = "abcde"
$StringOrg = $String

Global $Org[26] = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"]
Global $Rep[26] = ["e", "c", "c", "p", "a", "f", "b", "y", "i", "r", "z?", "l", "w", "u", "o", "d", "g", "j", "s", "t", "n", "v", "m", "k", "h", "z"]

ConsoleWrite(@CR)
For $i = 0 To 25
    $String = StringReplace($String, $Org[$i], $Rep[$i])
    ConsoleWrite($i & " - " & $String & @CR)
Next

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

read the comments in this code:

$String = StringSplit("test", "")
;replaces your inputbox just during testing
;StringSplit turns "test" into this array:
;$String[0] = 4
;$String[1] = "t"
;$String[2] = "e"
;$String[3] = "s"
;$String[4] = "t"

$Org = StringSplit("a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z", ",")
;$Org[0] = 26
;$Org[1] = "a"
;$Org[2] = "b"
;$Org[3] = "c"
;~~~~~~~~~~~~~
;$Org[26] = "z"

$Rep = StringSplit("e,c,c,p,a,f,b,y,i,r,z?,l,w,u,o,d,g,j,s,t,n,v,m,k,h,z,", ",", 1)
;$Rep[0] = 26
;$Rep[1] = "e"
;$Rep[2] = "c"
;$Rep[3] = "c"
;~~~~~~~~~~~~~
;$Rep[26] = "z"

$StringNew = ""
ConsoleWrite(@CR)
For $i = 1 To $String[0]
    ;the first time thru this loop $String[1] = "t"
    For $ii = 1 To $Org[0]
        ;run thru all of the original letters looking for "t"
        If $String[$i] = $Org[$ii] Then $StringNew &= $Rep[$ii]
        ;if you find a match, do the replacement & concatenation
        ;but your replacement is also the letter "t" :-(
        ;Mecrazycoder pointed out the letters that are the same
    Next
    ConsoleWrite($i & " - " & $StringNew & @CR)
Next
ConsoleWrite(@CR)

;or - a faster way

$StringNew = ""
ConsoleWrite(@CR)
For $i = 1 To $String[0]
    ;the first time thru this loop $String[1] = "t"
    $StringNew &= $Rep[Asc($String[$i]) - 96]
    ;Asc($String[$i]) = Asc("t") = 116
    ;116 - 96 = 20
    ;we want the 20th replacement letter :-( it is still "t"
    ConsoleWrite($i & " - " & $StringNew & @CR)
Next
ConsoleWrite(@CR)

[size="1"][font="Arial"].[u].[/u][/font][/size]

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...