anagram program

Vipershark

New member
Joined
Dec 11, 2006
Messages
4
Programming Experience
Beginner
I'm using VB.NET 2003.

I'm making a sort of anagrammer program, and I'm stuck. What I need help with is how to get up to 12 characters form a text box to randomize and display in 6 different labels, one randomization in each label.

Like if I type "there hello" in the text box, I want in one label "hello there" to display, "eohll ehtre" in the next label, ect. I have 6 labels, and I want something different to pop up in each each time you click a button. Can someone help?
 
There are probable people on this forum who'll give you some better code then this, but its better then nothing, this is to scramble the letters:

VB.NET:
        Dim chrText() As Char = TXTtext.Text
        Dim chrscrambled(UBound(chrText)) As Char
        Dim intExclude(UBound(chrText)) As Integer, j As Integer

        For i As Integer = 0 To UBound(chrText)
            Do
                Randomize()
                j = Rnd() * 100 Mod (UBound(chrText) + 1)
            Loop Until Array.IndexOf(intExclude, j) = -1
            intExclude(i) = j
            chrscrambled(i) = chrText(j - 1)
        Next

        Label1.Text = chrscrambled
 
Do you need to make considerations for if two labels turn up the same?

When you say up to 12 chars, do you want to (randomly?) get less than 12 charactors sometimes?
 
Last edited:
I think this is more efficient code and better, Luc :)
VB.NET:
    Function rndstring(ByVal input As String) As String
        Dim chars() As Char = input.ToCharArray
        Dim tempchar As Char
        Dim rnd As New Random
        Dim rndint As Integer
        For i As Integer = 0 To input.Length \ 2
            rndint = rnd.Next(input.Length \ 2, input.Length)
            tempchar = chars(i)
            chars(i) = chars(rndint)
            chars(rndint) = tempchar
        Next
        Return Convert.ToString(chars)
    End Function
You won't "ever" get duplicate random strings...
 
Thanks, that works great!

Now is it possible to do this same thing, but instead of just one combination popping up in a label, ALL of the possible combinations will pop up in a rich text box when entered? If possible, could you post that?
 
Poses some problem cause all the possible of a string with more then 10 characters are more then a billion :( But if you keep the string small my code should work, probable someone else with superior code here :rolleyes: :

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intCombinations As Integer = 1
        Dim ScrambledStrings(limit(txtString.Text.Length) - 1) As String
        Dim rndCombination As String
        ScrambledStrings(0) = rndstring2(txtString.Text)
        Do
            Do
                rndCombination = rndstring2(txtString.Text)
            Loop Until Array.IndexOf(ScrambledStrings, rndCombination) = -1
            ScrambledStrings(intCombinations) = rndCombination
            intCombinations += 1
            txtCombinations.AppendText(rndCombination & vbCrLf)
        Loop Until intCombinations = limit(txtString.Text.Length)
    End Sub

    Function rndstring2(ByVal input As String) As String
        Dim chrText() As Char = input.ToCharArray
        Dim chrscrambled(UBound(chrText)) As Char
        Dim intExclude(UBound(chrText)) As Integer, j As Integer

        For i As Integer = 0 To UBound(chrText)
            Do
                Randomize()
                j = Rnd() * 100 Mod (UBound(chrText) + 1)
            Loop Until Array.IndexOf(intExclude, j) = -1
            intExclude(i) = j
            chrscrambled(i) = chrText(j - 1)
        Next
        Return chrscrambled
    End Function

    Function limit(ByVal InputLength As Integer)
        Dim j As Long = 1
        For i As Integer = InputLength To 1 Step -1
            j *= i
        Next
        Return j
    End Function
 
Okay, that works good, but I have some problems. I decreased the character limit to 6. Whenever you try to scramble ore than 4 characters, it goes REEEEAAALLLY slow (and possibly freezes, dunno.).... Is there any way to add a progress bar that will fill from the point that you click the randomize button to the point when ALL of the combinations are finished? Also, when you put in 1 character to be randomized, it freezes. I put in a <= 9 error and that works, but only if you input a single number... letters and other characters don't work. It also freezes when no characters are inputted. I tried fixing that, but it didn't work. Can someone help?
 
About the progressbar thing:

VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim intCombinations As Integer = 1
        Dim ScrambledStrings(limit(txtString.Text.Length) - 1) As String
        Dim rndCombination As String
        ScrambledStrings(0) = rndstring2(txtString.Text)
        ProgressBar1.Minimum = 0
        ProgressBar1.Maximum = limit(txtString.Text.Length)
        ProgressBar1.Value = 0
        Do
            Do
                rndCombination = rndstring2(txtString.Text)
                Application.DoEvents()
            Loop Until Array.IndexOf(ScrambledStrings, rndCombination) = -1
            ScrambledStrings(intCombinations) = rndCombination
            intCombinations += 1
            txtCombinations.AppendText(rndCombination & vbCrLf)
            ProgressBar1.Value = intCombinations
        Loop Until intCombinations = limit(txtString.Text.Length)
    End Sub

    Function rndstring2(ByVal input As String) As String
        Dim chrText() As Char = input.ToCharArray
        Dim chrscrambled(UBound(chrText)) As Char
        Dim intExclude(UBound(chrText)) As Integer, j As Integer

        For i As Integer = 0 To UBound(chrText)
            Do
                Randomize()
                j = Rnd() * 100 Mod (UBound(chrText) + 1)
            Loop Until Array.IndexOf(intExclude, j) = -1
            intExclude(i) = j
            chrscrambled(i) = chrText(j - 1)
        Next
        Return chrscrambled
    End Function

    Function limit(ByVal InputLength As Integer)
        Dim j As Long = 1
        For i As Integer = InputLength To 1 Step -1
            j *= i
        Next
        Return j
    End Function

Although I think I'm doing it wrong cause if you want all possible combinations there are better ways to do it then generating it randomly and hoping that it differs from the rest, but atm I don't have that much time so if noone else replys then I'll help you :)
 
Okay, that works. It helps knowing that it doesn't totally freeze now, but I still can't tell if it's done. Is there a way to pop up a message box that says "Scrambling finished!" when all of the combinations are done? Also, I get this error when I use 6 characters and end the program:
Cannot access a disposed object named "RichTextBox"
errormsgbx8.png
 
Back
Top