Mike Bailey Posted February 13, 2004 Posted February 13, 2004 I�m trying to create a program that will reverse a string like->(ABC -->= CBA) But if there are any spaces in the string I need them removed. It would look like this -->(A B C --> = CBA) In my code I have handled reversing the string and converting it to all upper case. But for the life of me I can�t seem to remember how to remove the spaces. The purpose of this whole stupid thing is to see if a string is the same word or sentence backwards as it is forwards. I think it�s called a Paladrom Can anyone help????????? Dim mystring As String Dim newstring As String Dim backstring As String Dim mystringlenght As Integer Dim x As Integer TextBox1.Text = TextBox1.Text.ToUpper mystringlenght = TextBox1.TextLength For x = 1 To mystringlenght mystring = Mid(TextBox1.Text, x, 1) newstring = newstring & mystring Next For x = mystringlenght To 1 Step -1 mystring = Mid(TextBox1.Text, x, 1) backstring = backstring & mystring Next If backstring = TextBox1.Text Then MsgBox("YaHooo") Else MsgBox("BooHooo") End If Quote
Administrators PlausiblyDamp Posted February 13, 2004 Administrators Posted February 13, 2004 (edited) backstring.Replace(" ","") would remove the spaces, however here's a quicker way.... Dim backstring As String TextBox1.Text = TextBox1.Text.ToUpper.Replace(" ", "") Dim chars() As Char = TextBox1.Text Array.Reverse(chars) backstring = New String(chars) If backstring = TextBox1.Text Then MsgBox("YaHooo") Else MsgBox("BooHooo") End If Edited February 13, 2004 by PlausiblyDamp Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Mike Bailey Posted February 13, 2004 Author Posted February 13, 2004 Thanks Thanks for the great reply worked great. Quote
Mike Bailey Posted February 13, 2004 Author Posted February 13, 2004 This is what I ended up with what do you think This is what I ended up with what do you think?? It works fine. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim newstring As String Dim backstring As String Dim temp As String Dim mystringlenght As Integer Dim x As Integer temp = TextBox1.Text.ToUpper.Replace(" ", "") mystringlenght = TextBox1.TextLength For x = mystringlenght To 1 Step -1 backstring = Mid(temp, x, 1) newstring = newstring & backstring Next If newstring = temp Then MsgBox("YaHooo") Else MsgBox("BooHooo") End If End Sub End Class Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.