String manipulation is a core skill in software engineering. While simple string reversal is a built-in feature in most languages, reversing a string while **keeping the spaces at their original index locations** requires a more strategic, algorithmic approach.
In this guide, we will solve this common algorithm puzzle using a simple **bookmark-and-restore** technique in Java, explaining the logic through a practical bookshelf analogy.
Imagine you have a row of books spelling out the phrase: "I am a developer".
The books are separated by empty slots (spaces) at specific positions. If you simply reversed the entire shelf, the spaces would end up in the wrong places, breaking the word formatting.
To do this correctly, you follow this plan:
- Write down gap locations: Note the slots that have spaces. You write down: "Gaps are at slot 1, slot 4, and slot 6."
- Squish the books: Remove the empty spaces and slide all the books together into a single word:
"Iamadeveloper". - Reverse the books: Flip that single squished word completely backward:
"repolevedamaI". - Restore the gaps: Slide the books apart and put the empty space dividers back into the exact slots you noted in Step 1 (slot 1, slot 4, slot 6).
Your finished shelf now spells out: "r ep o levedamaI". The spaces are exactly where they started!
Walkthrough of the Main Method Scenario
Let's trace how the program executes step by step from the entry point:
We split the input string "I am a developer" into individual characters and check each one:
String[] split = input.split("");
for (int index = 0; index < split.length; ++index) {
if (!split[index].equals(" ")) {
letters.add(split[index]);
} else {
spaces.add(index);
}
}
For our input, the letters list receives all the non-space characters. The spaces list bookmarks the index indices: [1, 4, 6].
We combine the letters into a single string using a StringBuilder and reverse them:
StringBuilder stringBuilder = new StringBuilder();
letters.forEach(eachLetter -> stringBuilder.append(eachLetter));
stringBuilder.reverse();
The letters are squished to "Iamadeveloper" and then reversed to "repolevedamaI".
Finally, we loop through our bookmarked space indices and insert the spaces back into our reversed letters:
spaces.forEach(eachSpace -> stringBuilder.insert(eachSpace," "));
As spaces are inserted, the characters shift down automatically.
- Insert space at 1:
"r epolevedamaI" - Insert space at 4:
"r ep olevedamaI" - Insert space at 6:
"r ep o levedamaI"
Java Implementation
Below is the complete Java code showing how to map and reverse a string while keeping space formatting intact:
package io.practise.accolite;
import java.util.ArrayList;
import java.util.List;
public class ReversingWordBX {
public static void main(String[] args) {
String input = "I am a developer";
List<String> letters = new ArrayList<>();
List<Integer> spaces = new ArrayList<>();
String[] split = input.split("");
for (int index = 0; index < split.length; ++index) {
if (!split[index].equals(" ")) {
letters.add(split[index]);
} else {
spaces.add(index);
}
}
StringBuilder stringBuilder = new StringBuilder();
letters.forEach(eachLetter -> stringBuilder.append(eachLetter));
stringBuilder.reverse();
spaces.forEach(eachSpace -> stringBuilder.insert(eachSpace," "));
System.out.println(stringBuilder);
}
}
Conclusion
Solving formatting-preserving string puzzles is straightforward when you separate raw data from visual layout constraints. By bookmarking the space indices first, reversing the squashed letters, and then restoring the spaces, we avoid complex index arithmetic and deliver a clean, readable, and highly maintainable solution.