A String cannot be changed; a StringBuilder can, because it keeps its characters in an array with spare room. Write your own small version, MyBuilder, with a char[] buffer (starting capacity 8) and a counter size:
MyBuilder(String start);append(String)andappend(int)— both returnthis, so calls can be chained;reverse(),toLowerCase()— change the builder itself and returnthis;length(),capacity(),charAt(int),substring(int begin, int end),toString();- a private
ensureCapacity(int needed)that doubles the array until the characters fit.
MyBuilderApp reads the commands add word, number 42, reverse, lower, char 2, part 1 4, show until the end of the input, then prints the final content. For the input
add Hello
show
add World
number 2026
show
char 4
part 5 10
reverse
show
lower
the program prints
Hello (length 5, capacity 8)
HelloWorld2026 (length 14, capacity 16)
Character 4: o
Part 5..10: World
6202dlroWolleH (length 14, capacity 16)
Final: 6202dlrowolleh