All SolutionsAll Solutions
👉
Right-Align
Week 22, 2026
Python - Print Spaces and Single Loop | BMC | Python Solutions
def rightAlign(msg:str, lineWidth:int, startingChars:int, CharStep:int) -> None:
first = msg[0:startingChars]
spaces = lineWidth-startingChars
msg = msg[startingChars:len(msg)]
# print first line
print(" "*spaces, end="")
print(first)
# print rest
i = 0
while i < len(msg):
startingChars += CharStep
spaces = spaces-CharStep
print(" "*spaces, end="")
print(msg[i:i+startingChars])
i += startingChars
# main
msg = "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"
rightAlign(msg, 20, 1, 1)
print()
rightAlign(msg, 10, 1, 0)
print()
rightAlign(msg, 50, 50, -6)
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)
}
Recursive call (buggy on redirect = extra space) | Draw6 | C++ Solutions
#include "allheaders.hpp"
#include "main.hpp"
using namespace std;
//------------------------------------------------[VARIABLES AND FUNCTION DECLS]
string challengeStr3;
int lineWidth, startingChars, charStep;
string space = " ";
size_t position = 0;
bool firstPass = true;
void RightAlign(string& str, int lineWidth, int startingChars, int charStep, int position, int size, int charCount);
int main(int argc, char* argv[])
{
try {
cout << "\nChallenge pass1\n";
challengeStr3 = "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";
int size3 = challengeStr3.length();
firstPass = true;
lineWidth = 20;
startingChars = 1;
charStep = 1;
RightAlign(challengeStr3, lineWidth, startingChars, charStep, 0, size3, 0);
cout << "\nChallenge pass2\n";
challengeStr3 = "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";
firstPass = true;
lineWidth = 10;
startingChars = 1;
charStep = 0;
RightAlign(challengeStr3, lineWidth, startingChars, charStep, 0, size3, 0);
cout << "\nChallenge pass3\n";
challengeStr3 = "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";
firstPass = true;
lineWidth = 50;
startingChars = 50;
charStep = -6;
RightAlign(challengeStr3, lineWidth, startingChars, charStep, 0, size3, 0);
} // end of try
catch (const runtime_error& exception)
{
std::cout << "Msg: " << exception.what() << "\n";
}
return 0;
}
void RightAlign(string& str, int lineWidth, int startingChars, int charStep, int position, int size, int charCount)
{
if(firstPass)
{
int A = (lineWidth) - startingChars;
for(int a = 0; a <= A; a++)
{
cout << space;
position = A + startingChars;
firstPass = false;
}
cout << "\b";
cout << str.substr(0, startingChars) << "\n";
charCount = startingChars;
str.erase(0, charCount);
RightAlign(str, lineWidth,startingChars, charStep, position, str.length(), charCount);
}
else
{
int A = (lineWidth) - (charStep + charCount);
for(int a = 0; a <= A; a++)
{
cout << space;
firstPass = false;
}
cout << "\b";
cout << str.substr(0, charCount + charStep) << "\n";
position = A ;
charCount = lineWidth - position;
str.erase(0, charCount);
if(str.length() < 1)
{
return;
}
if(charCount < 0)
{
return;
}
RightAlign(str, lineWidth, startingChars, charStep, position, str.length(), charCount);
}
return ;
}