How to check whether each substring range (L to R) from an array of substring ranges is a palindrome in VB.NET

1 Answer

0 votes
Imports System
				
Module PalindromeChecker

    ' Check if a substring of str from left to right (inclusive) is a palindrome
    Function IsPalindrome(str As String, left As Integer, right As Integer) As Boolean
        While left < right
            If str(left) <> str(right) Then
                Return False
            End If
            left += 1
            right -= 1
        End While
        Return True
    End Function

    ' Check and print whether substrings defined by ranges are palindromes
    Sub CheckRangesForPalindrome(str As String, ranges As Integer(,))
		Dim rows As Integer = ranges.GetLength(0)
		
        For i As Integer = 0 To Rows - 1
            Dim startIdx As Integer = ranges(i, 0)
            Dim endIdx As Integer = ranges(i, 1)
            Dim substring As String = str.Substring(startIdx, endIdx - startIdx + 1)

            Console.Write(substring & ": ")
            If IsPalindrome(str, startIdx, endIdx) Then
                Console.WriteLine("Palindrome")
            Else
                Console.WriteLine("Not palindrome")
            End If
        Next
    End Sub

    Sub Main()
        Dim str As String = "abcddcbeebc"
        Dim ranges As Integer(,) = {
            {2, 5},
            {5, 10},
            {3, 7}
        }

        CheckRangesForPalindrome(str, ranges)
    End Sub

End Module


' run:
'
' cddc: Palindrome
' cbeebc: Palindrome
' ddcbe: Not palindrome
'

 



answered 8 hours ago by avibootz
edited 7 hours ago by avibootz

Related questions

...