Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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


  • Administrators
Posted (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 by PlausiblyDamp

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...