👉
Right-Align
Week 22
You are looking to create an ASCII art program that can take long strings of text like poems and quotes and right-align them according to some rules
The following should all be able to be specified:
- Line-width: this is the number of characters (columns) per line and is fixed for all lines
- Starting chars: the number of characters on the first line before wrapping
- Char step: the difference in character count between each line
For simplicity, you decide to treat spaces the same as any other character - you don't have to worry about trimming whitespace between words at the start/end of lines
Suppose we run our program with the string "abcdefghijklmnopqrstuvwxyz", a line width of 8, starting char count of 1 and char step of 1, we would get this:
Now suppose we used values of 20, 2, 4:
...or 20, 10, -2
If we try with the quote "There's poetry in nature. A symmetry - Saffron A. Kent" and values 5, 5, 0, we get:
Or 15, 1, 2:
For this challenge, you should use the following string:
You should run your program with the following parameters (i.e. 3 times total): 20, 1, 1, 10, 1, 0 and 50, 50, -6 - the combined output of running all 3 programs in that order should be paste below - you can separate each program's output with new lines if you want
Note: you should keep your program code, since you will need it in a future challenge
Hints
Hints will be released at the start of each of the following days - e.g. the start of day 3 is 48 hours after the challenge starts
| Release Day | Hint |
|---|---|
| 2 | Next week's challenge will take a lot longer - so this is hopefully an easier challenge - you can complete it with only 1 loop |
| 3 | You will want to keep looping until you reach the end of the string - a pre-conditional while loop will likely be best |
| 4 | Inside each loop iteration, you can either have another loop or use a built-in string repeat function if your language has it to output the current number of leading spaces (which you can calculate using the column count and current number of characters), followed by the substring from the current string position and number of characters |
| 5 | Update the offset and current number of characters - keep looping and outputting/appending to a local string variable until the string position has exceeded the string length |