I am learning Java and trying to reverse a string without using the built-in StringBuilder.reverse() method.
For example:
Input:
HelloExpected Output:
olleHI tried using a loop, but I am confused about the correct logic and the best approach.
InterviewsJava1 answer
I am learning Java and trying to reverse a string without using the built-in StringBuilder.reverse() method.
For example:
Input:
HelloExpected Output:
olleHI tried using a loop, but I am confused about the correct logic and the best approach.
public class ReverseString {
public static void main(String[] args) {
String str = "hello";
String reversedStr = "";
// Traverse the original string from end to beginning
for (int i = str.length() - 1; i >= 0; i--) {
reversedStr += str.charAt(i); // Append each character to reversedStr
}
// Print the original and reversed strings
System.out.println("Original String: " + str);
System.out.println("Reversed String: " + reversedStr);
}
}