yamachan
yamachan

Reputation: 1079

Golang: How to convert string to []int?

I tackle with this question.

I need to convert strings to int. In this case, I need to convert "5 2 4 6 1 3" to, for example, [6]int{5,2,4,6,1,3}. I wrote following this code, especially AizuArray(). It seems elements are int here. Would you please let me know if my way is correct? Or could you let me know the better ways? I ask this because I feel my way would be redundant and Java way is much easier. Thank you.

package main

import (
    "fmt"
    "reflect"
    "strconv"
    "strings"
)

func AizuArray(A string, N string) []int {
    a := strings.Split(A, " ")
    n, _ := strconv.Atoi(N) // int 32bit
    b := make([]int, n)
    for i, v := range a {
        b[i], _ = strconv.Atoi(v)
    }
    return b
}

func main() {
    A := "5 2 4 6 1 3"
    N := "6"
    j := strings.Split(A, " ")
    for _, v := range j {
        fmt.Println(reflect.TypeOf(v))
    }
    b := AizuArray(A, N)
    fmt.Println(b)
    for _, v := range b {
        fmt.Println(reflect.TypeOf(v))
    }
}

Upvotes: 5

Views: 24904

Answers (3)

User12547645
User12547645

Reputation: 8447

Here is a simpler example where you would not have to split the string:

str := "123456"
if _, err := strconv.Atoi(str); err != nil {
    // do stuff, in case str can not be converted to an int
}
var slice []int // empty slice
for _, digit := range str {
    slice = append(slice, int(digit)-int('0')) // build up slice
}

Why do you need the int('0')? Because int() will convert the character to the corresponding ASCII code (ascii table here). For 0 that would be 48. So you will have to substract 48 from whatever your digit corresponds to in "ascii decimal".

Upvotes: 1

hitesh_noty
hitesh_noty

Reputation: 385

Would you please let me know if my way is correct?

If you just want to convert string(space separated integers) to []int

func AizuArray(A string, N string) []int {
 a := strings.Split(A, " ")
 n, _ := strconv.Atoi(N) // int 32bit
 b := make([]int, n)
 for i, v := range a {
     b[i], err = strconv.Atoi(v)
     if err != nil {
        //proper err handling
        //either b[i] = -1 (in case positive integers)
     }
 }
 return b
}

then your approach is correct.

I tackle with this question.

In context of this question you want to take input from STDIN so should do,

package main

import (
    "fmt"
)

func insertionSort(arr []int) {
    //do further processing here.
   fmt.Println(arr)
}

func main() {
    var N int
    fmt.Scanf("%d", &N)
    b := make([]int, N)
    for iter:=0;iter<N;iter++ {
        fmt.Scanf("%d",&b[iter])
    }
    insertionSort(b)
}

Upvotes: 9

Darigaaz
Darigaaz

Reputation: 1500

I think you overcomplicating things unless I am missing something.

https://play.golang.org/p/HLvV8R1Ux-

package main

import (
    "fmt"
    "strings"
    "strconv"
)

func main() {
    A := "5 2 4 6 1 3"

    strs := strings.Split(A, " ")
    ary := make([]int, len(strs))
    for i := range ary {
        ary[i], _ = strconv.Atoi(strs[i])
    }

    fmt.Println(ary)    
}

Upvotes: 6

Related Questions