The main issue with your way of doing it is your taking the n'th character from str and appending it to make it the n'th character of res.

You could fix it like this:

public String reverseString(String str) {
   String res = "";

   for (int i = str.length() - 1; i >= 0; i--) {
       res = res + str.charAt(i);
   }
   return res;
}
Answer from Jack Gore on Stack Overflow
๐ŸŒ
BeginnersBook -
beginnersbook.com โ€บ home โ€บ java examples โ€บ java program to reverse words in a string
Java Program to reverse words in a String
September 15, 2017 - public class Example { public void reverseWordInMyString(String str) { /* The split() method of String class splits * a string in several strings based on the * delimiter passed as an argument to it */ String[] words = str.split(" "); String reversedString = ""; for (int i = 0; i < words.length; i++) { String word = words[i]; String reverseWord = ""; for (int j = word.length()-1; j >= 0; j--) { /* The charAt() function returns the character * at the given position in a string */ reverseWord = reverseWord + word.charAt(j); } reversedString = reversedString + reverseWord + " "; } System.out.println(str); System.out.println(reversedString); } public static void main(String[] args) { Example obj = new Example(); obj.reverseWordInMyString("Welcome to BeginnersBook"); obj.reverseWordInMyString("This is an easy Java Program"); } }
Discussions

how to reverse a string in java
Write down a short string on a piece of paper and reverse the string by hand. How do you do that? More on reddit.com
๐ŸŒ r/learnprogramming
11
1
April 4, 2021
reversing a string with while loop
Sounds like what you might like to try is start with your counter being the length of the string, then iterate over that until your counter is at the beginning of the string... At each iteration, you'll want to add a character to your new string. Look at String.charAt(int index). More on reddit.com
๐ŸŒ r/learnjava
7
1
October 24, 2018
๐ŸŒ
Simplilearn
simplilearn.com โ€บ home โ€บ resources โ€บ software development โ€บ how to reverse a string in java: 12 best methods
How to Reverse a String in Java: 12 Best Methods
May 5, 2025 - How to Reverse a String in Java? 1. Using toCharArray() 2. Using StringBuilder 3. Using While Loop/For Loop 4. Converting a String to Bytes 5. Using ArrayList
Address ย  5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ reverse-a-string-in-java
Reverse a String in Java - GeeksforGeeks
Explanation: StringBuilder objects are mutable, and their reverse() method reverses the content in-place, which is faster than manual looping. We can use character array to reverse a string. Follow Steps mentioned below: First, convert String to character array by using the built-in Java String class method toCharArray(). Then, scan the string from end to start, and print the character one by one. ... import java.io.*; class Main{ public static void main(String[] args){ String s = "Geeks"; // Using toCharArray to copy elements char[] arr = s.toCharArray(); for (int i = arr.length - 1; i >= 0; i--) System.out.print(arr[i]); } }
Published ย  October 14, 2025
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ practicing-string-operations-and-type-conversions-in-java โ€บ lessons โ€บ string-manipulation-splitting-and-reversing-words-in-java
String Manipulation: Splitting and Reversing Words in Java
Finally, we need to consolidate these reversed words into a single string, separated by spaces. We can achieve this using the StringBuilder class in Java. Here's how we do that: First, we append the first word to the StringBuilder, then append a space followed by the next word in a loop.
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_howto_reverse_string.asp
Java How To Reverse a String
String originalStr = "Hello"; String ... Explanation: We start with an empty string reversedStr. - On each loop, we take one character from the original string using charAt()....
Find elsewhere
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2012 โ€บ 01 โ€บ how-to-reverse-string-in-java-using.html
How to Reverse String in Java Using Iteration and Recursion - Example
I was looking for simple Java program to reverse String in Java when I find this great tutorial. This tutorial not only explains how to reverse String in Java with simple example of recursion and for loop but also explains logic behind it.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 449084 โ€บ java โ€บ enhanced-loop-iterate-reverse-order
Using enhanced for loop to iterate in reverse order ? (Java in General forum at Coderanch)
As far as I know, not at all. The for-each loop only goes in one direction. ... Amandeep: You'd have to go through the steps of creating another List/Array, copying the elements in reverse order to the new List/Array, then using the for each loop on that. It should be relatively easy to do.
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how to reverse a string in java
r/learnprogramming on Reddit: how to reverse a string in java
April 4, 2021 -

hey guys i need your help in java so i have a very beginner problem and i'll probably be laughed at for asking this but how do you reverse a string in java

so far the online called I've been given to work with is:

import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
char[] arr = text.toCharArray();

//your code goes here

}
}

i don't know how to go about it, all i have to do is reverse the string. uhg it was so easy in python with just the (::-) is that how its written idk it's been some time python.

anyways i would appreciate any help and advice on learning java effectively i don't know much yet, i tried going on leetcode but daaaaamn it was crazy hard, i didn't manage to do any problem. thank you

๐ŸŒ
LeetCode
leetcode.com โ€บ problems โ€บ reverse-string
Reverse String - LeetCode
The input string is given as an array of characters s. You must do this by modifying the input array in-place [https://en.wikipedia.org/wiki/In-place_algorithm] with O(1) extra memory.
๐ŸŒ
coderolls
coderolls.com โ€บ reverse-a-string-in-java
How To Reverse A String In Java (5 ways) - Coderolls
December 2, 2019 - Using for loop set all the elements of the stringByteArray in reverse order with reversedStringByteArray. Create a new String object using reversedStringByteArray and assign it to the reversedString. Print reversedString. The program to reverse the string using getBytes() method of the Java ...
๐ŸŒ
Quora
quora.com โ€บ How-can-a-string-be-reversed-in-Java
How can a string be reversed in Java? - Quora
Answer (1 of 3): // Sample program: import java.lang.*; // java.lang.System && java.lang.StringBuilder class StringReverse { public static void main(String[] args) { String network_search = "lssnepo nasegurum"; System.out.println( "My network_search: " + new StringBuilder( network_search )...
๐ŸŒ
Sololearn
sololearn.com โ€บ en โ€บ Discuss โ€บ 2805778 โ€บ how-to-reverse-a-string-in-java
How to reverse a string in java | Sololearn: Learn to code for FREE!
June 7, 2021 - public class Program { public static ... reversed += arr[i]; } System.out.println(reversed); } } Use the for loop starting from the last item in the array and work backwards....
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ reverse-string-java
Reverse a String in Java (with Examples)
September 28, 2024 - Using a for loop, iterate through the array of words in reverse order. Add a space after each word in the StringBuilder. Finally, use the toString() method to convert the StringBuilder to a string.
๐ŸŒ
Moometric
moometric.com โ€บ development โ€บ java โ€บ reverse-a-string-in-java
Reverse a String in Java - MooMetric.com
October 23, 2015 - Another method which is also similar but uses the Java StringBuilder class instead: ... Very similar to the previous method, the only difference is that we are using the โ€œappendโ€ function of the Java StringBuilder class in the iteration loop instead to concatenate the characters to form the reversed String.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ dsa โ€บ reverse-a-string
Reverse a String โ€“ Complete Tutorial - GeeksforGeeks
The idea is to start at the last character of the string and move backward, appending each character to a new string res. This new string res will contain the characters of the original string in reverse order.
Published ย  October 3, 2025
๐ŸŒ
Medium
medium.com โ€บ womenintechnology โ€บ 3-ways-to-reversing-string-in-java-b40a23bbcc6c
3 ways to Reversing String In JAVA | by Daily Debug | Women in Technology | Medium
August 10, 2023 - String str = "Reverse This String"; String words[] = str.split(" "); StringBuffer strReversed = new StringBuffer(); for(int i=0;i< words.length;i++){ for(int j=words[i].length()-1;j>-1;j--){ char [] wordsCharArray = words[i].toCharArray(); strReversed.append(wordsCharArray[j]); } strReversed.append(" "); } System.out.print(strReversed);...
๐ŸŒ
CodeGym
codegym.cc โ€บ java blog โ€บ random โ€บ different ways to reverse a string in java
Different Ways to Reverse a String in Java
February 1, 2023 - This technique is one of the simplest ways to reverse a string in Java by using a for loop. We can iterate through the characters of the string from the end to the beginning, and add them to a new string variable.
๐ŸŒ
OneCompiler
onecompiler.com โ€บ java โ€บ 3zq9u7j7z
Reverse the string without using loop - Java - OneCompiler
import java.util.*; public class Main { public static void main(String[] args) { Scanner dataInput = new Scanner(System.in); String inputString = dataInput.nextLine(); System.out.println("INput - "+inputString); inputString = reverseString(inputString); System.out.println("OutPut - "+inputString); } private static String reverseString(String inputString){ int length = inputString.length(); if(length == 1){ return inputString; } return inputString.charAt(length - 1) + reverseString(inputString.substring(0,length-1)); } } ... Write, Run & Share Java code online using OneCompiler's Java online compiler for free. It's one of the robust, feature-rich online compilers for Java language, running the Java LTS version 17. Getting started with the OneCompiler's Java editor is easy and fast.