Skip to main content

Reverse string using recursion in

Vb program for Reverse string using recursion. Here problem description and explanation.

' Include namespace system
Imports System 
'  Reverse string using recursion in

Public Class ReverseString
    '  This is reversing the string elements recursively
    Public Function  reverseText(ByVal text As String, 
                                 ByVal location As Integer) As String
        '  Base condition to stop the recursion process
        if (location >= 0) Then
            '  Recursive function call
            Return  text(location).ToString() + 
              Me.reverseText(text, location - 1)
        End If
        '  When no character remaining
        Return  ""
    End Function
    '  This is handling the request process of reverse string elements
    Public Function  reverse(ByVal text As String) As String
        '  Display given string elements
        Console.WriteLine("Before Text : [" + text + "]")
        Return  Me.reverseText(text, text.Length - 1)
    End Function
    Public Shared Sub Main(ByVal args As String())
        Dim task As ReverseString = New ReverseString()
        Dim text As String = "ABCDE"
        text = task.reverse(text)
        '  After reverse	
        Console.WriteLine("After Text  : [" + text + "]"& vbLf )
        text = task.reverse("654A321")
        '  After reverse	
        Console.WriteLine("After Text  : [" + text + "]"& vbLf )
    End Sub
End Class


Before Text : [ABCDE]
After Text  : [EDCBA]

Before Text : [654A321]
After Text  : [123A456]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment