computergroove

Trying to put characters in a string in order

12 posts in this topic

I have a input box that I wand to order the characters in a msgbox ie: AABBCCBBAA would return AAAABBBBCC. What I have tried is not working.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Global $len, $Answer

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 1156, 123, 254, 124)
$Input1 = GUICtrlCreateInput("Input1", 8, 48, 1137, 21)
$Go = GUICtrlCreateButton("Go", 8, 80, 113, 25)
$Label1 = GUICtrlCreateLabel("Enter a string of characters and click go. This program will order the characters like AaBbCc etc.", 8, 24, 456, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Go
            $UserInput = GUICtrlRead($Input1);get user input
            $len = StringLen($UserInput);count number of characters
            Ascii()
    EndSwitch
WEnd

Func Ascii()
    $AscStart = 65
    For $i = 65 To 90;characters A - Z
        $StringTemp = $UserInput
;~      MsgBox(0,"Original String",$StringTemp)
            Do
                $TempString = StringInStr($StringTemp,Chr($i),1);Number where the character exists
                $StringTemp = StringTrimLeft($StringTemp,$TempString - 1);Strip characters on left side where first string is located
                $StringLeft = StringLeft($StringTemp,1)
                $Answer = $Answer & $StringLeft;Add character to answer
                $StringTemp = StringTrimLeft($StringTemp,1)
            Until $TempString == 0
    Next
MsgBox(0,0,$Answer)
EndFunc

 


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites



I got it. I wanted to reorder the characters from a sentence to look like AAaaBBbbCCccDDdd etc. Upper case A's first followed by lower case a's. This finally worked however I can tell that there is a bug somewhere because I had to use the if statement to prevent rogue A's from appearing at the end of the characters as it was counting them. Can you remove the if statement and make the answer come out correctly?

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Global $len, $Answer = "", $AsciiValue = 65

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 1156, 123, 254, 124)
$Input1 = GUICtrlCreateInput("Input1", 8, 48, 1137, 21)
$Go = GUICtrlCreateButton("Go", 8, 80, 113, 25)
$Label1 = GUICtrlCreateLabel("Enter a string of characters and click go. This program will order the characters like AaBbCc etc.", 8, 24, 456, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Go
            $UserInput = GUICtrlRead($Input1);get user input
            $len = StringLen($UserInput);count number of characters
            Ascii()
    EndSwitch
WEnd

Func Ascii()
    For $i=1 To 52
        $StringTemp = $UserInput
            Do
                $TempString = StringInStr($StringTemp,Chr($AsciiValue),1);Number where the character exists
                $StringTemp = StringTrimLeft($StringTemp,$TempString - 1);Strip characters on left side where first string is located
                $StringLeft = StringLeft($StringTemp,1)
                If $StringLeft == Chr($AsciiValue) Then
                    $Answer = $Answer & $StringLeft;Add character to answer
                EndIf
                $StringTemp = StringTrimLeft($StringTemp,1)
            Until $TempString == 0

                If $AsciiValue <= 96 Then
                    $AsciiValue = $AsciiValue + 32
                Else
                    $AsciiValue = $AsciiValue - 31
                EndIf
    Next
                MsgBox(0,0,$Answer)
EndFunc

 


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Idk if you would want to do this but you could split the user input into an array and do bubble sorting.

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

Global $string = "ASDLFJHASLDJHFKJAWEJQzCMXNZMDBlkhjaskdjfasIOJAOSDFJIOjalsdfiashdfiouhqwpoJAOWPIJASDLFJASDNlnasdfkjansdkjfnasdkKLJASDKLJFHASDKJFHASDlsajpofdsjhfaowsdefaoidsnLAZZSSzzSDFZKSJALKSDJFHALSDFHJK"
Global $string_array = StringSplit($string, "", $STR_CHRSPLIT + $STR_ENTIRESPLIT + $STR_NOCOUNT)
Global $ascii_array = StringToASCIIArray($string)

For $i = 0  to UBound($string_array) - 1
    For $p = 0 to UBound($string_array) - 1
        ; First sort by the ascii value, this will order it all capital letters A - Z
        If ($ascii_array[$i] < $ascii_array[$p]) Then
            _ArraySwap($string_array, $i, $p)
            _ArraySwap($ascii_array, $i, $p)
        EndIf
        
        ; Then sort by character value, this will move all the lowercase letters up below the upper case characters
        If ($string_array[$i] < $string_array[$p]) Then
            _ArraySwap($string_array, $i, $p)
            _ArraySwap($ascii_array, $i, $p)
        EndIf
    Next
Next

_ArrayDisplay($string_array)
MsgBox("", "", _ArrayToString($string_array, ""))

 

Edited by InunoTaishou

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

I haven't really checked what InunoTaishou provided, but it is probably okay by my quick look at it.

I however would have done it differently, which is perfectly normal, as there is usually more than one way to code these things.

So I will just suggest, if you want to look further afield, that you look at _ArraySort from the UDF section of the Help file, for starters.
You could use that with some elaborate case comparison code, to get the result you want ... perhaps some shuffle up for lower case letters, as required.

Edited by Santa

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Make sure brain is in gear before opening mouth!
Remember, what is not said, can be just as important as what is said.

Spoiler

What is the Secret Key? Life is like a Donut

If I put effort into communication, I expect you to read properly & fully, or just not comment.
Ignoring those who try to divert conversation with irrelevancies.
If I'm intent on insulting you or being rude, I will be obvious, not ambiguous about it.
I'm only big and bad, to those who have an over-active imagination.

I may have the Artistic Liesense ;) to disagree with you. TheSaint's Toolbox (be advised many downloads are not working due to ISP screwup with my storage)

userbar.png

Share this post


Link to post
Share on other sites

This could be done to check also non word chars (or not) depending on whether you put a condition (or not)
According to Santa's cogitations  :)

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <Array.au3>

$Form1 = GUICreate("Form1", 1156, 123, 254, 124)
$Input1 = GUICtrlCreateInput("", 8, 48, 1137, 21)
$Go = GUICtrlCreateButton("Go", 8, 80, 113, 25)
$Label1 = GUICtrlCreateLabel("Enter a string of characters and click go. This program will order the characters like AaBbCc etc.", 8, 24, 456, 17)
GUISetState()

GuiCtrlSetData($Input1, _ 
    "DKJFHASDlsajpofdsjhfaowsdefaoidsnLAZZSSzzSDFZKSJAL, StringInStr($StringTemp,Chr($AsciiValue),1);" )

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Go
             Msgbox(0,"", _Sort(GUICtrlRead($Input1)) )
    EndSwitch
WEnd

Func _Sort($txt)
    Local $a = StringSplit($txt, ""), $res
    _ArrayColInsert($a, 1)
    For $i = 1 to $a[0][0]
      $n = Asc(StringLower($a[$i][0]))
      $a[$i][1] = (StringIsUpper($a[$i][0])) ? $n-0.5 : $n
    Next   
    _ArraySort($a, 0, 1, 0, 1)  
    ; _ArrayDisplay($a)
    For $i = 1 to $a[0][0]
       If StringIsAlpha($a[$i][0]) Then $res &= $a[$i][0]  ; condition here
    Next   
    Return $res
EndFunc

 

1 person likes this

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Nice mikell !

Another way, probably slower...

#Include <Array.au3>
Local $res
$string = "ASDLFJHASLDJHFKJAWEJQzCMXNZMDBlkhjaskdjfasIOJAOSDFJIOjalsdfiashdfiouhqwpoJAOWPIJASDLFJASDNlnasdfkjansdkjfnasdkKLJASDKLJFHASDKJFHASDlsajpofdsjhfaowsdefaoidsnLAZZSSzzSDFZKSJALKSDJFHALSDFHJK"

MsgBox(0, "", _Sort($string) )


Func _Sort($txt)
    $s = Execute("''" & StringRegExpReplace($txt, "(.)", "&';$1' & (StringIsUpper('$1') ? '' : '.')"))
    $a = StringRegExp($s, ";(.\.?)", 3)
    _ArraySort($a)

    For $i = 0 to UBound($a) - 1
       $res &= StringLeft($a[$i], 1)
    Next  
    Return $res
EndFunc

 

Edit :

Finally, Execute is not needed, this should suffice

#Include <Array.au3>
Local $res
$string = "ASDLFJHASLDJHFKJAWEJQzCMXNZMDBlkhjaskdjfasIOJAOSDFJIOjalsdfiashdfiouhqwpoJAOWPIJASDLFJASDNlnasdfkjansdkjfnasdkKLJASDKLJFHASDKJFHASDlsajpofdsjhfaowsdefaoidsnLAZZSSzzSDFZKSJALKSDJFHALSDFHJK"

MsgBox(0, "", _Sort($string) )


Func _Sort($txt)
    Local $s = StringRegExpReplace($txt, "[[:lower:]]\K", ".")
    Local $a = StringRegExp($s, "[[:lower:]]\.|.", 3)
    _ArraySort($a)

    For $i = 0 to UBound($a) - 1
       $res &= StringLeft($a[$i], 1)
    Next  
    Return $res
EndFunc

 

Edited by jguinch
1 person likes this

Share this post


Link to post
Share on other sites

I made this for fun. Just a simple mod to a bubblesort.

#include <Array.au3>

Local  $sString = "ASDLFJHASLDJHFKJAWEJQzCMXNZMDBlkhjaskdjfasIOJAOSDFJIOjalsdfiashdfiouhqwpoJAOWPIJASDLFJASDNlnasdfkjansdkjfnasdkKLJASDKLJFHASDKJFHASDlsajpofdsjhfaowsdefaoidsnLAZZSSzzSDFZKSJALKSDJFHALSDFHJK"

ConsoleWrite("!Normal String" & @CRLF)
ConsoleWrite($sString & @CRLF)
ConsoleWrite("!New String" & @CRLF)
ConsoleWrite(BubbleSortMod($sString) & @CRLF)

Func BubbleSortMod($smyString)
    $bs_array=StringSplit($smyString,"",2)
    For $i = UBound($bs_array) - 1 To 1 Step -1
        For $j = 2 To $i
            If asc($bs_array[$j - 1]) > asc($bs_array[$j]) Then
                $temp = $bs_array[$j - 1]
                $bs_array[$j - 1] = $bs_array[$j]
                $bs_array[$j] = $temp
            EndIf
        Next
    Next
    Return _ArrayToString($bs_array,"")
EndFunc   ;==>BubbleSort

Saludos

1 person likes this

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Danyfirex,
It looks a lot like a former _ArraySort() reinvention - and btw it gives the same result, which is not exactly what the OP asked for  :)

jguinch,
clever  ;)  Melba would call such a way 'lateral thinking'

Edited by mikell

Share this post


Link to post
Share on other sites

Danyfirex,
It looks a lot like a former _ArraySort() reinvention - and btw it gives the same result, which is not exactly what the OP asked for  :)

jguinch,
clever  ;)  Melba would call such a way 'lateral thinking'

aaaaa I didn't read post two lol. I'll rewrite it.

 

Saludos

Share this post


Link to post
Share on other sites

My way.

#include <String.au3>

Global $sMainString = "ASDLFJHASLDJHFKJAWEJQzCMXNZMDBlkhjaskdjfasIOJAOSDFJIOjalsdfiashdfiouhqwpoJAOWPIJASDLFJASDNlnasdfkjansdkjfnasdkKLJASDKLJFHASDKJFHASDlsajpofdsjhfaowsdefaoidsnLAZZSSzzSDFZKSJALKSDJFHALSDFHJK"


ConsoleWrite("!Normal String" & @CRLF)
ConsoleWrite($sMainString & @CRLF)
ConsoleWrite("!Sorted String" & @CRLF)
ConsoleWrite(_SortChars($sMainString) & @CRLF)


Func _SortChars($sString)
    Local Const $sChars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    Local $sOutString = ""
    Local $sChar = ""
    For $i = 1 To StringLen($sChars)
        $sChar = StringMid($sChars, $i, 1)
        StringReplace($sString, $sChar, "", 0, 1)
        If @extended Then
            $sOutString &= _StringRepeat($sChar, @extended)
        EndIf
    Next
    Return $sOutString
EndFunc   ;==>_SortChars

Saludos

2 people like this

Share this post


Link to post
Share on other sites

@DannyFirex I modified your code slightly to accept userinput:

#include <String.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

Global $Answer

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 1156, 123, 254, 124)
$Input1 = GUICtrlCreateInput("Input1", 8, 48, 1137, 21)
$Go = GUICtrlCreateButton("Go", 8, 80, 113, 25)
$Label1 = GUICtrlCreateLabel("Enter a string of characters and click go. This program will order the characters like AaBbCc etc.", 8, 24, 456, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Go
            $sMainString = GUICtrlRead($Input1);get user input
            $len = StringLen($sMainString);count number of characters
            $Answer = _SortChars($sMainString)
            MsgBox(0,"",$Answer)
    EndSwitch
WEnd

Func _SortChars($sString)
    Local Const $sChars = $sMainString
    Local $sOutString = ""
    Local $sChar = ""
    For $i = 1 To StringLen($sChars)
        $sChar = StringMid($sChars, $i, 1)
        StringReplace($sString, $sChar, "", 0, 1)
        If @extended Then
            $sOutString &= _StringRepeat($sChar, @extended)
        EndIf
    Next
    Return $sOutString
EndFunc   ;==>_SortChars

and it returns too many characters. I dont believe that worked.


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

You've deleted my constant string :S. Fixed

#include <MsgBoxConstants.au3>
#include <String.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>

Global $Answer

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 1156, 123, 254, 124)
$Input1 = GUICtrlCreateInput("Input1", 8, 48, 1137, 21)
$Go = GUICtrlCreateButton("Go", 8, 80, 113, 25)
$Label1 = GUICtrlCreateLabel("Enter a string of characters and click go. This program will order the characters like AaBbCc etc.", 8, 24, 456, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $Go
            $sMainString = GUICtrlRead($Input1);get user input
            $len = StringLen($sMainString);count number of characters
            $Answer = _SortChars($sMainString)
            MsgBox(0,"",$Answer)
    EndSwitch
WEnd

Func _SortChars($sString)
    Local Const $sChars = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
    Local $sOutString = ""
    Local $sChar = ""
    For $i = 1 To StringLen($sChars)
        $sChar = StringMid($sChars, $i, 1)
        StringReplace($sString, $sChar, "", 0, 1)
        If @extended Then
            $sOutString &= _StringRepeat($sChar, @extended)
        EndIf
    Next
    Return $sOutString
EndFunc   ;==>_SortChars

 

The most important part:

My Nick name is Danyfirex (just one n) lol

Saludos

Edited by Danyfirex

Share this post


Link to post
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