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 Overflowjava - Reversing a String Without Reverse Function or loops - Stack Overflow
Reversing a String without Using any predefined method (including: charAt(), length())
Reverse a string in Java without StringBuilder - Stack Overflow
Explanation for how this recursive method to reverse a string works (JAVA)
Videos
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
This answer assumes the following:
Can't use built-in
reverse()function, such as found onStringBuilderandCollections.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
My friends attended an interview and the panel asked this questions.. if it is possible or not to reverse a string without using any predefined method. As i mentioned in the title.. You cannot use any predefined method like .charAt() or .length().
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.