Ben
Ben

Reputation: 1

Reversing Digits

I'm trying to make a function that takes a three digit number and reverses it (543 into 345)
I can't take that value from a TextBox because I need it to use the three numbers trick to find a value.

RVal = ReverseDigits(Val)
Diff = Val - RVal
RDiff = ReverseDigits(Diff)
OVal = Diff + RDiff

543-345=198
198+891=1089

Then it puts 1089 in a TextBox

Function ReverseDigits(ByVal Value As Integer) As Integer
    ' Take input as abc
    ' Output is (c * 100 + b * 10 + a) = cba
    Dim ReturnValue As Boolean = True
    Dim Val As String = CStr(InputTextBox.Text)
    Dim a As Char = Val(0)
    Dim b As Char = Val(1)
    Dim c As Char = Val(2)

    Value = (c * 100) + (b * 10) + (a)

    Return ReturnValue
End Function

I've tried this but can't figure out why it won't work.

Upvotes: 0

Views: 364

Answers (2)

jmcilhinney
jmcilhinney

Reputation: 54417

Here's a method I wrote recently when someone else posted basically the same question elsewhere, probably doing the same homework:

Private Function ReverseNumber(input As Integer) As Integer
    Dim output = 0

    Do Until input = 0
        output = output * 10 + input Mod 10
        input = input \ 10
    Loop

    Return output
End Function

That will work on a number of any length.

Upvotes: 0

djv
djv

Reputation: 15774

You can convert the integer to a string, reverse the string, then convert back to an integer. You may want to enforce the three digit requirement. You can validate the argument before attempting conversion

Public Function ReverseDigits(value As Integer) As Integer
    If Not (value > 99 AndAlso value < 1000) Then Throw New ArgumentException("value")
    Return Integer.Parse(New String(value.ToString().Reverse().ToArray()))
End Function

My code is pretty simple and will also work for numbers that don't have three digits assuming you remove that validation. To see what's wrong with your code, there are a couple of things. See the commented lines which I changed. The main issue is using Val as a variable name, then trying to index the string like Val(0). Val is a built in function to vb.net and the compiler may interpret Val(0) as a function instead of indexing a string.

Function ReverseDigits(ByVal Value As Integer) As Integer
    ' Dim ReturnValue As Boolean = True
    ' Dim Val As String = CStr(InputTextBox.Text)
    Dim s As String = CStr(Value)
    Dim a As Char = s(0)
    Dim b As Char = s(1)
    Dim c As Char = s(2)

    Value = Val(c) * 100 + Val(b) * 10 + Val(a)

    'Return ReturnValue
    Return Value
End Function

(Or the reduced version of your function, but I would still not hard-code the indices because it's limiting your function from expanding to more or less than 3 digits)

Public Function ReverseDigits(Value As Integer) As Integer
    Dim s = CStr(Value)
    Return 100 * Val(s(2)) + 10 * Val(s(1)) + Val(s(0))
End Function

And you could call the function like this

Dim inputString = InputTextBox.Text
Dim inputNumber = Integer.Parse(inputString)
Dim reversedNumber = ReverseDigits(inputNumber)

Bonus: If you really want to use use math to find the reversed number, here is a version which works for any number of digits

Public Function ReverseDigits(value As Integer) As Integer
    Dim s = CStr(value)
    Dim result As Integer
    For i = 0 To Len(s) - 1
        result += CInt(Val(s(i)) * (10 ^ i))
    Next
    Return result
End Function

Upvotes: 2

Related Questions