Hi everyboy! I ran into this assignment that asks me to "create an application that allows the user to enter a word or phrse, then the Label should display that word or phrase backwards in all lowercase.

For example, I enter "I read"
Then the label should display "daer I"

I try to do this with "Substring, Replace, etc", butnone of them work.

Is there any particular method to achieve this assignment?

Dani AI

Generated

The assignment is simple: take the user input, show it reversed and in lowercase. Note the example in the post ("I read" -> "daer I") contradicts "all lowercase" — the lowercase result should be "daer i". Several good options have already been suggested; here are practical choices, pitfalls, and a couple of small VB snippets you can drop into a form handler.

As pointed out, reversing an array of chars is concise. As warned, repeatedly concatenating strings inside a loop is slow; prefer a StringBuilder if you use a loop. If the teacher expects a loop (for learning), this loop is clear and efficient:

Dim input As String = TextBox1.Text
If String.IsNullOrEmpty(input) Then
    Label1.Text = ""
    Return
End If

Dim lower As String = input.ToLowerInvariant()
Dim sb As New System.Text.StringBuilder(lower.Length)
For i As Integer = lower.Length - 1 To 0 Step -1
    sb.Append(lower(i))
Next
Label1.Text = sb.ToString()

For production code be aware of two things: culture and Unicode. Use ToLowerInvariant to avoid culture-specific surprises (Turkish dotted/undotted I). See String.ToLowerInvariant. Also, reversing by code unit will break emoji and combining marks; if you need to preserve grapheme clusters, enumerate text elements with StringInfo and reverse those instead. Example approach:

Imports System.Globalization

Dim text As String = TextBox1.Text.ToLowerInvariant()
Dim elems As New List(Of String)
Dim e = StringInfo.GetTextElementEnumerator(text)
While e.MoveNext()
    elems.Add(CStr(e.Current))
End While
elems.Reverse()
Label1.Text = String.Concat(elems)

For small, school-level strings any simple method will do. For real apps choose clarity plus correctness: StringBuilder for loops, array/LINQ for compact code, and StringInfo when Unicode correctness matters. See the StringBuilder docs for performance notes: https://learn.microsoft.com/en-us/dotnet/api/system.text.stringbuilder?view=net-7.0.

Recommended Answers

All 6 Replies

you can deconstruct the old string starting from the back and create a new string.


just roughly, untested.


e.g.

for x = 0 to oldstring .length -1
     Newstring &= oldstring.substring(x,1)
next

or something along those lines

Sub Main()

        Dim original As String = "Reverse Me"
        Dim temp As Char() = original.ToCharArray()
        Array.Reverse(temp)
        Dim reversed As String = New String(temp)
        Console.WriteLine(reversed)
        Console.Read()

    End Sub

That is cool. I made a hangman program for my daughter, and I used a loop to pull out each character into an array. ToCharArray would have been much better!

>> Ranx

Concatenating a string in a loop like that kills performance on an application. You should use a StringBuilder or take the approach apegram did. Every time you append a character it reallocates a new piece of memory for stringsize+1. Now for small strings you can't really tell, but when they get bigger it can wreak havoc on your application.

you can ue this

Dim R = Label1.Text.ToCharArray.Reverse
Label1.Text = String.Join("", R.Cast(Of String).ToArray)

but I'm sure your teacher wants you to do it the hard way (using loops)
to give you better understanding

Hey sknake

thanks mate, am aware of the shortcomings of strings, though was unaware of the flexibility the ToCharArray method (Will be looking into that more :-). was just trying to point the original poster in some sort of direction while i downed my morning coffee.

:-)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.