locked
Anagram Checker RRS feed

  • Question

  • Hi all,

    I am currently creating an anagram checker, but my code isn't working. I am running into an issue using a loop.

    The idea behind the code is that it randomises the first string over and over again until it is equal to the second string, therefore confirming that they are anagrams of eachother.

    Public Class Form1
    	Dim firstword As String
    	Dim firstwordscramble As String
    	Dim secondword As String
    	Dim i As Integer = 0
    	Dim anagram As Boolean
    	Public Function Scramble(ByVal phrase As String) As String
    		Static rand As New Random()
    		Return New String(phrase.ToLower.ToCharArray.OrderBy(Function(r) rand.Next).ToArray)
    	End Function
    
    	Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
    		firstword = txtFirstword.Text
    		secondword = txtSecondword.Text
    		Do Until i = -1
    			If Scramble(firstword) = secondword Then
    				anagram = True
    			Else
    				anagram = False
    			End If
    		Loop
    		If anagram = True Then
    			MsgBox("Anagram")
    		Else
    			MsgBox("Not Anagram")
    		End If
    	End Sub
    End Class

    Monday, October 1, 2018 12:24 PM

All replies

  • Hi all,

    I am currently creating an anagram checker, but my code isn't working. I am running into an issue using a loop.

    The idea behind the code is that it randomises the first string over and over again until it is equal to the second string, therefore confirming that they are anagrams of eachother.

    Public Class Form1
    	Dim firstword As String
    	Dim firstwordscramble As String
    	Dim secondword As String
    	Dim i As Integer = 0
    	Dim anagram As Boolean
    	Public Function Scramble(ByVal phrase As String) As String
    		Static rand As New Random()
    		Return New String(phrase.ToLower.ToCharArray.OrderBy(Function(r) rand.Next).ToArray)
    	End Function
    
    	Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
    		firstword = txtFirstword.Text
    		secondword = txtSecondword.Text
    		Do Until i = -1
    			If Scramble(firstword) = secondword Then
    				anagram = True
    			Else
    				anagram = False
    			End If
    		Loop
    		If anagram = True Then
    			MsgBox("Anagram")
    		Else
    			MsgBox("Not Anagram")
    		End If
    	End Sub
    End Class

    From what I see you are not changing the value of i anywhere in your code after you have initialized it as 0

    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Monday, October 1, 2018 12:32 PM
  • The loop is supposed to be infinite. Initially I wanted the section of code to loop until the scrambled version of the first word was equal to the second word, but it didn't work either:

    Do Until Scramble(firstword) = secondword
    			If Scramble(firstword) = secondword Then
    				anagram = True
    			Else
    				anagram = False
    			End If
    		Loop
    		If anagram = True Then
    			MsgBox("Anagram")
    		Else
    			MsgBox("Not Anagram")
    		End If

    Monday, October 1, 2018 12:48 PM
  • Hello,

    This is a great opportunity to set strategic breakpoints and then hover over variables to see what's going on.  While using breakpoints take advantage also of the local window found under the debug IDE menu. Also you can set conditional breakpoints  (the link explains them well) starting with VS2015.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, October 1, 2018 12:48 PM
  • Hello,

    It's could be done in very different way.

    Here is one of them:

    class Class2 { const int CharsCount = 256; int[] count = new int[CharsCount]; public bool IsFullAnagram(string pOne, string pTwo) { for (int i = 0; i < CharsCount; ++i) count[i] = 0; HandleString(pOne, +1); HandleString(pTwo, -1); int anagram = 0; foreach (int i in count)

    anagram += count[i]; return anagram == 0; } private void HandleString(string pString, int step) { byte[] line = Encoding.ASCII.GetBytes(pString); foreach (byte b in line) { count[b] += step; } } }



    Sincerely, Highly skilled coding monkey.

    Monday, October 1, 2018 4:07 PM
  • Hi, sorry for the late reply.

    I didn't consider a lot of these points that you have brought up while I was making the program.

    And I wasn't receiving any error messages, the program was simply 'hanging' so I kept having to use the stop button in visual studio.

    I actually ended up changing my mind and doing this problem using CharArray's and ended up getting a working solution, however a lot of the things you said are valid and I will take into consideration in future projects.

    Thanks.

    Monday, October 1, 2018 4:54 PM
  • Hi,

    Do you resolve the issue?We appreciated you shared us your solution and mark it as an answer.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, October 9, 2018 2:05 AM