Reversing a string means reading its characters in the opposite order. The character at the end moves to the beginning, and the character at the beginning moves to the end.
For example, if the input string is "hello", the reversed string will be "olleh".
In this guide, we will reverse a string from scratch in Python and Java. We will not use built-in reverse methods such as Python slicing [::-1]or Java's StringBuilder.reverse().
Best Approach to Reverse a String
A simple and efficient way to reverse a string is the two-pointer technique. This method uses two index positions:
- One pointer starts from the first character.
- Another pointer starts from the last character.
- The characters at both positions are swapped.
- The pointers then move toward the middle.
This continues until both pointers meet or cross each other. At that point, the character order has been reversed.
The two-pointer method is commonly used because it is easy to understand and works in linear time.
Python Code to Reverse a String Without Built-in Methods
text = "hello"
characters = []
for ch in text:
characters.append(ch)
left = 0
right = len(characters) - 1
while left < right:
temp = characters[left]
characters[left] = characters[right]
characters[right] = temp
left = left + 1
right = right - 1
reversed_text = ""
for ch in characters:
reversed_text = reversed_text + ch
print(reversed_text)
In this Python program, we first create an empty list named characters. Then we copy each character from the string into that list.
We use a list because Python strings are immutable. This means a string character cannot be changed directly by using an index.
After the characters are stored in a list, we place one pointer at the start and another pointer at the end. On every loop, we swap the characters and move both pointers closer to the center.
When the loop finishes, the list contains the characters in reverse order. Finally, we build the reversed string manually by joining each character through a loop.
Java Code to Reverse a String Without Built-in Methods
public class Main {
public static void main(String[] args) {
String text = "hello";
char[] characters = new char[text.length()];
for (int i = 0; i < text.length(); i++) {
characters[i] = text.charAt(i);
}
int left = 0;
int right = characters.length - 1;
while (left < right) {
char temp = characters[left];
characters[left] = characters[right];
characters[right] = temp;
left = left + 1;
right = right - 1;
}
String reversedText = "";
for (int i = 0; i < characters.length; i++) {
reversedText = reversedText + characters[i];
}
System.out.println(reversedText);
}
}
In the Java program, we create a character array with the same length as the original string. Then we copy every character from the string into the array using charAt().
A character array is used because Java strings cannot be modified directly. Once the characters are inside the array, we can swap them using indexes.
The left pointer begins at the first index, and the right pointer begins at the last index. The loop swaps the characters at these two indexes until the middle of the array is reached.
At the end, we create a new string by adding each character from the reversed array.
Dry Run Example
Let us walk through the input "hello" to understand how the two-pointer method works.
Input: hello Initial characters: h e l l o First swap: Swap h and o o e l l h Second swap: Swap e and l o l l e h The middle character l stays in the same place. Final reversed string: olleh
Time and Space Complexity
The time complexity of this solution is O(n), where n is the number of characters in the string. Each character is processed a small number of times.
The space complexity is O(n) because we store the characters in an additional array or list before reversing them.
Output
olleh
Why Learn This Method?
Although many programming languages provide short ways to reverse a string, learning the manual method is important for beginners. It teaches how indexes work, how character arrays are used, and how swapping helps solve problems efficiently.
This same two-pointer idea is also useful in many other coding problems, such as checking palindromes, reversing arrays, and solving interview-style string questions.
Reversing a string without built-in methods is a simple problem, but it builds a strong foundation in loops, indexes, arrays, and algorithmic thinking.