XXXX
XXXX

Reputation: 247

Reverse int golang

How to change 12345 to 54321? With a string, you can change the string to a rune, and reverse it, but you cannot do the same for an integer. I have searched and found no one talking about this. Examples
131415 >>> 514131
1357 >>> 7531
123a >>> ERROR
-EDIT-
I was thinking, why not create a slice and index that?
Then I realized that you can't index int
(http://play.golang.org/p/SUSg04tZsc)
MY NEW QUESTION IS
How do you index an int? OR
How do you reverse a int?

Upvotes: 3

Views: 10765

Answers (6)

Nisal Edu
Nisal Edu

Reputation: 7591

Try this

package main

import "fmt"

func reverse(x int) int {
    num := x
    returnValue := 0
    if num < 0 {
        num *= -1
    }
    for num > 0 {
        returnValue = (returnValue * 10) + (num % 10)
        num /= 10
    }
    if x < 0 {
        returnValue *= -1
    }
    return returnValue
}

func main() {
    fmt.Println(reverse(1235))
}

Live code - https://go.dev/play/p/_KvYAzGb5Dg

Upvotes: 1

ndiecodes
ndiecodes

Reputation: 11

Similar to Fokiruna's answer but also checks for a 32bit overflow

func reverse(x int) int {
    result, sign := 0, 1
 
    if(x < 0) {
        sign = -1
        x = -x     
    }

    for x > 0 {
        remainder := x % 10;
        result = result * 10 + remainder
        x = x/10
    }
    
    var checkInt int = int(int32(result))

    if checkInt != result {
        return 0
    }

    return result * sign
}

Upvotes: 1

Fokiruna
Fokiruna

Reputation: 149

Also flips negative numbers int

func Abs(x int) int {
  if x < 0 {
    return -x
  }
   return x
}

func reverse_int(n int) int {
  newInt := 0
  sign := 1
   if n < 0 {
    sign = -1
   }
   n = Abs(n)
  for n > 0 {
    remainder := n % 10
    newInt = newInt*10 + remainder
    n /= 10
  }
  return newInt * sign 
}

func main() {
  fmt.Println(reverse_int(-100))
  fmt.Println(reverse_int(-1001))
  fmt.Println(reverse_int(131415))
  fmt.Println(reverse_int(1357))
}

Upvotes: 1

pwaterz
pwaterz

Reputation: 731

Very similar to the first answer but this checks to make sure you don't go out of bounds on the type.

func reverse(x int) int {    
    rev := 0
    for x != 0 {
        pop := x % 10
        x /=  10
        if rev > math.MaxInt32/10 || (rev == math.MaxInt32 /10 && pop > 7) {
            return 0
        }
        if rev < math.MinInt32/10 || (rev == math.MinInt32/10 && pop < -8) {
            return 0
        }
        rev = rev * 10 + pop
    }

    return rev
}

Upvotes: 4

Wade73
Wade73

Reputation: 4509

I converted the integer to a string, reverse the string, and convert the result back to a string.

package main

import (
    "fmt"
    "strconv"
)

func main() {

    fmt.Println(reverse_int(123456))
        fmt.Println(reverse_int(100))
        fmt.Println(reverse_int(1001))
        fmt.Println(reverse_int(131415))
        fmt.Println(reverse_int(1357))

}

func reverse_int(value int) int {

    intString := strconv.Itoa(value)

    newString := ""

    for x := len(intString); x > 0; x-- {
        newString += string(intString[x - 1])
    }

    newInt, err := strconv.Atoi(newString)

    if(err != nil){
        fmt.Println("Error converting string to int")
    }

    return newInt
}

Upvotes: 4

Akavall
Akavall

Reputation: 86248

Here is a solution that does not use indexing an int

package main

import (
    "fmt"
)

func reverse_int(n int) int {
    new_int := 0
    for n > 0 {
        remainder := n % 10
        new_int *= 10
        new_int += remainder 
        n /= 10
    }
    return new_int 
}

func main() {
    fmt.Println(reverse_int(123456))
    fmt.Println(reverse_int(100))
    fmt.Println(reverse_int(1001))
    fmt.Println(reverse_int(131415))
    fmt.Println(reverse_int(1357))
}

Result:

654321
1
1001
514131
7531

Go playground

Upvotes: 15

Related Questions