How to convert a character (flatten) matrix to a single string in Go

1 Answer

0 votes
package main

import (
    "fmt"
    "strings"
)

func flattenMatrix(matrix [][]rune) string {
    var b strings.Builder

    for _, row := range matrix {
        for _, r := range row {
            b.WriteRune(r)
        }
    }

    return b.String()
}

func main() {
    matrix := [][]rune{
        {'H', 'e', 'l', 'l', 'o'},
        {' ', 'W', 'o', 'r', 'l', 'd'},
        {'!'},
    }

    output := flattenMatrix(matrix)
    fmt.Println("Flattened string:", output)
}

 
 
/*
run:
 
Flattened string: Hello World!
 
*/

 



answered Jan 10 by avibootz

Related questions

...