What do you mean for "without reverse function or loops". First of all in java are called methods, not functions, and second in some way you need to iterate the strings. This code just does the job with a recursive method. Call it from your constructor and youโ€™re done. There aren't any already built-in java methods to do what you want.

public class HelloWorld{


public String recursiveReverse(String[] words,StringBuilder b,int length){
     if (length < 0) 
        return b.toString();
     else{
        b.append(words[length] + " ");
        length--;
        return recursiveReverse(words,b,length);
     }
            
}

public String reverse (String input){
   String[] words = input.split(" ");
   StringBuilder reverse = new StringBuilder();
   return recursiveReverse(words,reverse,words.length-1);
}

     public static void main(String []args){
        HelloWorld a = new HelloWorld();
        System.out.println(a.reverse("hello this is a test reverse this string"));
     }
}

Output:

string this reverse test a is this hello
Answer from Virgula on Stack Overflow
๐ŸŒ
Quora
quora.com โ€บ How-do-I-reverse-a-String-in-Java-without-using-any-loop-or-inbuilt-methods
How to reverse a String in Java without using any loop or inbuilt methods - Quora
Quora is a place to gain and share knowledge. It's a platform to ask questions and connect with people who contribute unique insights and quality answers.
๐ŸŒ
Blogger
javahungry.blogspot.com โ€บ 2013 โ€บ 06 โ€บ reverse-or-mirroring-string-without.html
How to Reverse a String without using reverse built in method or recursion in Java : Source code with example | Java Hungry
public class Reverse { static int i,c=0,res; static void stringreverse(String s) { char ch[]=new char[s.length()]; for(i=0;i < s.length();i++) ch[i]=s.charAt(i); for(i=s.length()-1;i>=0;i--) System.out.print(ch[i]); } public static void main (String args[]) { System.out.println("Original String is : "); System.out.println(" manchester united is also known as red devil "); Reverse.stringreverse(" manchester united is also known as red devil "); } } ... Subham Mittal has worked in Oracle for 3 years. Enjoyed this post? Never miss out on future posts by subscribing JavaHungry
Discussions

java - Reversing a String Without Reverse Function or loops - Stack Overflow
So I have to reverse a string but like an example would be reverse Hello World. Well my code reverses it like this dlroW olleH and you have to reverse it like World Hello. This is my code below can More on stackoverflow.com
๐ŸŒ stackoverflow.com
Reversing a String without Using any predefined method (including: charAt(), length())
Well, it depends on what they actually mean with "predefined method". Strings are immutable and you can't get the contents of a string in a way that makes it reversible unless you use at least length and charAt or toCharArray. Excluding solutions using Reflection or Unsafe, that would be possible but not sensible. More on reddit.com
๐ŸŒ r/javahelp
38
3
March 25, 2021
Reverse a string in Java without StringBuilder - Stack Overflow
I am trying to reverse a string WITHOUT using StringBuilder. I have written the below code but it is giving an error as soon as it hits the loop. The error is Exception in thread "main" java.lang. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Explanation for how this recursive method to reverse a string works (JAVA)
Try walking through it on paper. Here's an algorithm to follow: Reverse: input is "str". If str is blank, return blank. separate str into the first character, car, and the remaining characters, cdr. Invoke this algorithm with an input of cdr, label result "reversedCdr". Return reversedCdr + car. If you do this by hand, you'll see what's happening. Make sure to carefully track the stack. Here's an example. Reverse(str is "nab") str isn't blank, keep going car is "n", cdr is "ab". Call reverse(str is "ab") str isn't blank, keep going car is "a", cdr is "b" Call reverse(str is "a") str isn't blank, keep going car is "b", cdr is "" Call reverse(str is "") str is blank, return blank. Return "" + "b", "b" Return "b" + "a", "ba" Return "ba" + "n", "ban" More on reddit.com
๐ŸŒ r/learnprogramming
3
5
February 21, 2021
Top answer
1 of 4
2

What do you mean for "without reverse function or loops". First of all in java are called methods, not functions, and second in some way you need to iterate the strings. This code just does the job with a recursive method. Call it from your constructor and youโ€™re done. There aren't any already built-in java methods to do what you want.

public class HelloWorld{


public String recursiveReverse(String[] words,StringBuilder b,int length){
     if (length < 0) 
        return b.toString();
     else{
        b.append(words[length] + " ");
        length--;
        return recursiveReverse(words,b,length);
     }
            
}

public String reverse (String input){
   String[] words = input.split(" ");
   StringBuilder reverse = new StringBuilder();
   return recursiveReverse(words,reverse,words.length-1);
}

     public static void main(String []args){
        HelloWorld a = new HelloWorld();
        System.out.println(a.reverse("hello this is a test reverse this string"));
     }
}

Output:

string this reverse test a is this hello
2 of 4
1

This answer assumes the following:

  • Can't use built-in reverse() function, such as found on StringBuilder and Collections.

  • Can't use loops.

  • Input can contain any number of words.

  • Words are separated by a single space.

First, the easiest solution would be to split() the input and then use Stream logic to combine the words in reverse order, however I consider Stream logic to be "Loop logic", so that won't do.

That leaves the use of a recursive method as a solution, similar to the attempt in the question:

static String reverseWords(String input) {
    int idx = input.indexOf(' ');
    if (idx == -1)
        return input;
    return reverseWords(input.substring(idx + 1)) + ' ' + input.substring(0, idx);
}

Test

System.out.println(reverseWords("Hello World"));
System.out.println(reverseWords("The quick brown fox jumps over the lazy dog"));

Output

World Hello
dog lazy the over jumps fox brown quick The
๐ŸŒ
Medium
medium.com โ€บ @thurumerla.venkatesh โ€บ 5-ways-to-reverse-a-string-in-java-without-using-reverse-or-sort-methods-66bc995a5cc9
10 Different Ways to Reverse a String in Java (Without using reverse or sort methods) | by Thurumerla Venkatesh | Medium
February 19, 2024 - Here, weโ€™ll explore ten simple and basic approaches to reversing string using lambdas and streams, without relying on built-in methods like reverse or sort.
๐ŸŒ
Medium
medium.com โ€บ @salmanshaik3y โ€บ reversing-a-string-without-using-inbuilt-methods-2007d160f160
Reversing a String Without Using Inbuilt Methods โ€” Basic Java Programs for Interviews | by SalmanTests | Medium
June 13, 2024 - Reversing a string without using built-in methods involves a straightforward approach of traversing the string from end to beginning and appending characters to form the reversed string.
Find elsewhere
๐ŸŒ
PREP INSTA
prepinsta.com โ€บ home โ€บ dsa in java โ€บ java program to reverse a string
Reverse a String in Java | PrepInsta
August 8, 2025 - What is the most efficient way to reverse a string in Java? The most efficient way is using StringBuilder or StringBuffer with the .reverse() method. Itโ€™s fast, uses less memory, and is preferred in real world applications.
๐ŸŒ
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
In this case when the recursion function is called, it will continuously go in a loop and won't terminate. You probably want some checks in there or else I'd say you failed that interview question ;) ... Anonymous said... Writing Java program to reverse a String without using reverse function or using recursion is just too popular and every body knows how to do that.
๐ŸŒ
Medium
medium.com โ€บ @bhagyashree25.sahu โ€บ reverse-a-string-in-java-without-using-inbuilt-methods-and-with-inbuilt-methods-0624dd1727ff
Reverse a String in Java: Without Using Inbuilt Methods and With Inbuilt Methods | by Bhagyashree Sahu | Medium
June 14, 2024 - Hereโ€™s how you can reverse a string without using inbuilt methods: ... The original string str is defined. An empty StringBuilder object reverseString is initialized to build the reversed string.
๐ŸŒ
Interviewing.io
interviewing.io โ€บ questions โ€บ reverse-string
How to Reverse a String [Interview Question + Solution]
September 13, 2018 - To reverse a string in place means to modify the original string directly without using any additional memory. There are two ways to do this: the first is iterative, and the second uses recursion.
๐ŸŒ
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
1 month ago - 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
Top answer
1 of 2
1
The previous solution given will work! I'm going to suggest some modifications. Here's the original:public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.substring(i, i + 1); } return reversed;}That call to str.substring(i, i + 1) is essentially a way of picking out a single character at index i, but there's actually a built in String method for that: charAt. And I think things read a bit more easily using it. Using it is also a bit semantically clearer in my opinion (others can disagree!):public static String reverse(String str) { String reversed = ""; for (int i = str.length() - 1; i >= 0; i--) { reversed += str.charAt(i); } return reversed;}It's generally better to use a StringBuilder, though, since that keeps us from generating a lot of Strings along the way (what if our input String was a very long String)?public static String reverse(String str) { StringBuilder reversed = new StringBuilder(); for (int i = 0; i < str.length(); ++i) { reversed.insert(0, str.charAt(i)); } return reversed.toString();}Note that here, I actually iterated forward through the String and prepended (using insert), but you could iterate backwards and use the append method, too. But the main point is that using a StringBuilder keeps us from having to generate new Strings for each character in the input.I hope that helps!
2 of 2
0
We can't use a pre-defined function in java that can instantaneously reverse a string. Therefore, we must come up with a way to do so. One way we can do this is by creating a new string adding individual characters to it by traversing through the original string in reverse order. This will result in the final string being the reverse of the original string, and this will be our solution to the problem. An implementation of this solution looks like:public class MyClass {public static void main(String args[]) {String before="hello";String after=reverse(before);System.out.println(after);}public static String reverse(String str){String reversed="";for(int i=str.length()-1; i>=0; i--){reversed+=str.substring(i,i+1);}return reversed;}}
Top answer
1 of 1
2

The last index in an array is not array.length but array.length - 1. Arrays are indexed on a zero-base, the first index is 0.

An array with two elements for example has indices [0] and [1], not [2].

You access, in the first iteration, stringChars[index2] and index2 = length where length = myString.length(). Thus the IndexOutOfBoundException. Carefully read through your code and analyze which indices you need. Create a small example, use some small print statements to debug your code and see which indices you are actually using.


Here is an example for a more compact reverse algorithm:

char[] input = ...

// Iterate in place, from both sides at one time
int fromFront = 0;
int fromEnd = input.length - 1;

while (fromFront < fromEnd) {
    // Swap elements
    char temp = input[fromEnd];
    input[fromEnd] = input[fromFront];
    input[fromFront] = temp;

    fromFront++;
    fromEnd--;
}

The algorithm swaps the element from the first position with the element from the last position in place. Then it moves one forward swapping the second element with the second to last and so on. It stops once both indices meet each other (if length is odd) or if the first index gets greater then the other (if length is even).

A more easy version, however without in-place, is to create a new array:

char[] input = ...
char[] reversedInput = new char[input.length];

// Reversely iterate through source
int forwardIndex = 0;
for (int i = input.length - 1; i > 0; i--) {
    reversedInput[forwardIndex] = input[i];
    forwardIndex++;
}
๐ŸŒ
Quora
quora.com โ€บ How-can-I-reverse-a-string-without-an-inbuilt-function
How to reverse a string without an inbuilt function - Quora
Answer (1 of 2): in C lang it would be : #include #include int main() { char* name = "chandradhar"; printf("the actual str is %s\n",name); int len = 0; for(int j = 0;name[j]!='\0';j++){ len++; } //printf("the len of str is %d\n",len); char* revName = (char*)malloc(s...
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ explanation for how this recursive method to reverse a string works (java)
r/learnprogramming on Reddit: Explanation for how this recursive method to reverse a string works (JAVA)
February 21, 2021 -
public String reverse(String str) {
    if(str.length()==0){
        return str;
    }
    else{
        return reverse(str.substring(1))+str.charAt(0);
    }
}

I was wondering how recursive methods work. What causes this method continue to plug itself back into the function? When does str.length==0 and why does it end up returning a reversed string rather than a blank in my main method.

๐ŸŒ
Medium
medium.com โ€บ @harishchaudhary โ€บ how-to-reverse-string-in-java-86ca8b4be793
How to reverse String in Java. How to reverse String in Java is aโ€ฆ | by Harish Chaudhary | Medium
October 3, 2022 - String in Java without using StringBuffer or they will ask you to write an iterative reverse function which reverses string in Java. In this tutorial, we will see how to reverse a string in a both iterative and recursive fashion...
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ What-is-a-way-to-reverse-a-string-without-using-recursion-or-library-functions
What is a way to reverse a string without using recursion or library functions? - Quora
Answer (1 of 2): Here is a suggestion (in BASIC): START INPUT โ€œEnter your string: โ€œ; A$ N = LEN (A$) โ€˜ Establishes the length of the string. DIM(new$, n) โ€˜ Create a new string with length n. FOR i = 1 to N new$(i) = MID$(A$, N - i + 1, 1) โ€˜ Read the characters of โ€˜A$โ€™ into โ€˜new$โ€™ ...
๐ŸŒ
FavTutor
favtutor.com โ€บ blogs โ€บ reverse-string-java
Reverse a String in Java (with Examples)
September 28, 2024 - The string is one of those data structures highly preferred by any Java developer while programming. Therefore, in this article, various methods to reverse the string in Java with example code and output.