Java Program to Reverse Characters in a String While Preserving Space Positions Interview Question
This question Asked by TechMahindra, LTIMindTree, Wipro
Explanation of the Logic
The given Java program reverses the characters of a string while keeping the spaces in their original positions. Here’s a step-by-step breakdown of how the program works:
Step-by-Step Breakdown:
Input String and Initialization:
The program starts by initializing a string str with the value "i am pocket". It then converts this string into a character array c using str.toCharArray(). This allows individual manipulation of each character in the string. Another character array ch of the same length as str is created to store the result. Initially, this array is empty.
Preserve Spaces in the Result Array:
The first for loop iterates through the character array c. If a space character (' ') is found in c, it is directly copied to the same position in the ch array. This step ensures that all spaces from the original string str are preserved in their original positions in the ch array.
Reverse Non-Space Characters:
The program initializes an integer j to point to the end of the ch array (c.length - 1).
A nested for-each loop iterates through each character a in the array c.
If the character a is not a space (' '), the program looks for the next available position in ch from the end (j) where a non-space character can be placed. If ch[j] is already a space, the inner while loop decrements j until a non-space position is found.
The character a is then placed at position j in the ch array, and j is decremented to continue filling characters from the end.
Output the Result:
Finally, the program prints the ch array, which contains the reversed characters of the original string with the spaces preserved in their original positions.
Example Execution
Let's walk through the program with the example string "i am pocket":
Original String: "i am pocket"
Character Array c: ['i', ' ', ' ', 'a', 'm', ' ', 'p', 'o', 'c', 'k', 'e', 't']
Result Array ch (after preserving spaces): ['_', ' ', ' ', '_', '_', ' ', '_', '_', '_', '_', '_', '_']
(The underscores represent empty spots) Reversing Characters While Preserving Spaces:
Start from the end (j = 11), and the first character from the end of c that is not a space is 't', so it is placed at ch[11]. Next, 'e' is placed at ch[10], 'k' at ch[9], and so on. The spaces in ch remain untouched.
Final Result:
ch = ['t', ' ', ' ', 'e', 'k', ' ', 'p', 'o', 'c', 'm', 'a', 'i']
Printed Output:
"t ek copmai"
Corrections and Improvements
The program has a logical flaw due to the misplaced loops. To correctly reverse the characters, the outer loop should handle the character placement only once, rather than iterating over i multiple times. Here’s an optimized version:
Corrected Logic and Result
This corrected version efficiently reverses the characters while preserving space positions without the redundant outer loop iterations. The result for the input "i am pocket" will still be:
Comments
Post a Comment