Odin SolutionsOdin Solutions

👉

Right-Align

Week 22, 2026

All Solutions

String Builder shenanigans | greenya | Odin Solutions

package main import "core:fmt" import "core:strings" main :: proc () { text_1 := "abcdefghijklmnopqrstuvwxyz" text_2 := "There's poetry in nature. A symmetry - Saffron A. Kent" text_3 := "We do not inherit the Earth from our ancestors; we borrow it from our " + "children. Every part of this Earth is sacred, and we must care for it " + "as we would care for our own lives - Chief Seattle" inputs: [] struct { text : string, line_width : int, starting_chars : int, char_step : int, } = { { text_1, 8, 1, 1 }, { text_1, 20, 2, 4 }, { text_1, 20, 10, -2 }, { text_2, 5, 5, 0 }, { text_2, 15, 1, 2 }, { text_3, 20, 1, 1 }, { text_3, 10, 1, 0 }, { text_3, 50, 50, -6 }, } for i in inputs { fmt.printfln("Input: %#v", i) fmt.println() fmt.println("Result:") fmt.println("|0--------|10-------|20-------|30-------|40-------|50-------|60----") fmt.println(right_align(i.text, i.line_width, i.starting_chars, i.char_step)) fmt.println("|---------|---------|---------|---------|---------|---------|------") fmt.println() } } right_align :: proc (text: string, line_width, starting_chars, char_step: int) -> string { sb := strings.builder_make() written_chars := 0 max_chars := starting_chars for c, i in text { if written_chars == 0 { if i > 0 do strings.write_byte(&sb, '\n') starting_spaces := line_width - max_chars for _ in 0..<starting_spaces do strings.write_byte(&sb, ' ') } strings.write_byte(&sb, byte(c)) written_chars += 1 if written_chars == max_chars { written_chars = 0 max_chars += char_step } } return strings.to_string(sb) }