Jump to content

count letters in a word


Recommended Posts

thanks for the quick responses.

i have a text document with 500+ employee's names and trying to create a script which will scramble the words... like a game... and output to another text document for staff to play.

original text document (correct names):

bruce wayne

peter parker

bruce banner

...

new text document (scrambled):

eurbc neywa =

eetpr rkrape =

bceur nneabr =

...

I am unsure which approach to take to achieve this and if their may already be a simple String command which may easily achieve this.

Link to comment
Share on other sites

first you want to separate the first and last name so they don't get mixed. So you want to use string split

Next you want to split up the letters using a for loop and stringtrimleft or right.

Then use random to scramble them up.

The For loop to get the letters will probably take some work, just look up the functions in the help file

Giggity

Link to comment
Share on other sites

Why do random when you can have fun? ^_^

#include <INet.au3>
#include <String.au3>
#include <Array.au3>

_Main()

Exit

Func _Main()
    Local $aNames[3][2] = [["bruce wayne", ""], ["peter parker", ""], ["bruce banner", ""]], $i
    For $i = 0 To 2
        $aNames[$i][1] = _GetAnagram($aNames[$i][0])
    Next
    _ArrayDisplay($aNames)
EndFunc

Func _GetAnagram($sString)
    $sString = StringReplace($sString, " ", "+")
    Local $sURL = "http://www.anagramgenius.com/server.php?source_text=" & $sString & "&emphasis=1&gender=2&vulgar=0&seen=true"
    Local $sSource = _INetGetSource($sURL)
    If @error Then Return SetError(1, 0, "")
    Local $aResults = StringRegExp($sSource, "(?U)<span class=""black-18"">.*</span>", 3)
    If @error Then Return SetError(2, 0, "")
    Local $aAnagrams = _StringBetween($aResults[1], "<span class=""black-18"">'", ".'</span>")
    If @error Then Return SetError(3, 0, "")
    Return SetError(0, 0, StringLower($aAnagrams[0]))
EndFunc

WBD

Link to comment
Share on other sites

I was bored, so...

#include <Array.au3>

Dim $aResult[1]

$sInfile = @ScriptDir & "\names.txt"
$sOutfile = @ScriptDir & "\scramble.txt"

$hInfile = FileOpen($sInfile, 0)
$hOutfile = FileOpen($sOutfile, 2)

If $hInfile <> -1 And $hOutfile <> -1 Then
    While 1
        $sFullname = FileReadLine($hInfile)
        If @error Then ExitLoop
        $aFullname = StringSplit($sFullname, " ")
        If IsArray($aFullname) Then
            ReDim $aResult[$aFullname[0]]
            For $i=1 To $aFullname[0]
                $aResult[$i - 1] = _ScrambleWord($aFullname[$i])
            Next
            FileWriteLine($hOutfile, _ArrayToString($aResult, " "))
        EndIf
    WEnd
    FileClose($hInfile)
    FileClose($hOutfile)
EndIf

    
Func _ScrambleWord($sWord)
    Local $aScramble[1]
    Local $aWord = StringSplit($sWord, "", 2)
    If IsArray($aWord) Then
        While UBound($aWord) > 1
            $iIdx = Random(0, UBound($aWord) - 1, 1)
            _ArrayAdd($aScramble, $aWord[$iIdx])
            _ArrayDelete($aWord, $iIdx)
        WEnd
        _ArrayAdd($aScramble, $aWord[0])
        _ArrayDelete($aScramble, 0)
        Return _ArrayToString($aScramble, "")
    EndIf
    Return ""
EndFunc
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...