Here's a solution.
private static void reverseRecursively(char[] myCharArr, int next) {
if (next >= myCharArr.length)
return;
// use recusion BEFORE printing in order to print in reversed order
reverseRecursively(myCharArr, next+1);
System.out.println(myCharArr[next]);
}
First call to the method should use 0 for the "next" index:
reverseRecursively(myCharArr, 0);
Answer from ArneHugo on Stack OverflowVideos
Here's a solution.
private static void reverseRecursively(char[] myCharArr, int next) {
if (next >= myCharArr.length)
return;
// use recusion BEFORE printing in order to print in reversed order
reverseRecursively(myCharArr, next+1);
System.out.println(myCharArr[next]);
}
First call to the method should use 0 for the "next" index:
reverseRecursively(myCharArr, 0);
You can probably use System.arraycopy
You can make use of StringBuilder#reverse() method like this:
String reverse = new StringBuilder(new String(letters)).reverse().toString();
I believe what you wrote is the signature of the method you have to create.
public void printReverse(char[] letters, int size){
//code here
}
You would have to iterate the array and print what it contains backwards. Use a reverse "for loop" to go through each item in "letters". I'll let you combine these yourself as it's an assignment. Here's an example of a for loop:
for (int i = array.length-1; i >= 0 ; i--){
System.out.print(array[i]);
}
The function takes the first character of a String - str.charAt(0) - puts it at the end and then calls itself - reverse() - on the remainder - str.substring(1), adding these two things together to get its result - reverse(str.substring(1)) + str.charAt(0)
When the passed in String is one character or less and so there will be no remainder left - when str.length() <= 1) - it stops calling itself recursively and just returns the String passed in.
So it runs as follows:
reverse("Hello")
(reverse("ello")) + "H"
((reverse("llo")) + "e") + "H"
(((reverse("lo")) + "l") + "e") + "H"
((((reverse("o")) + "l") + "l") + "e") + "H"
(((("o") + "l") + "l") + "e") + "H"
"olleH"
You need to remember that you won't have just one call - you'll have nested calls. So when the "most highly nested" call returns immediately (when it finds just "o"), the next level up will take str.charAt(0) - where str is "lo" at that point. So that will return "ol".
Then the next level will receive "ol", execute str.charAt(0) for its value of str (which is "llo"), returning "oll" to the next level out.
Then the next level will receive the "oll" from its recursive call, execute str.charAt(0) for its value of str (which is "ello"), returning "olle" to the next level out.
Then the final level will receive the "oll" from its recursive call, execute str.charAt(0) for its value of str (which is "hello"), returning "olleh" to the original caller.
It may make sense to think of the stack as you go:
// Most deeply nested call first...
reverse("o") -> returns "o"
reverse("lo") -> adds 'l', returns "ol"
reverse("llo") -> adds 'l', returns "oll"
reverse("ello") -> adds 'e', returns "olle"
reverse("hello") -> adds 'h', returns "olleh"
The best way is not to use recursion. These stuff are usually used to teach students the recursion concept, not actual best practices. So the way you're doing it is just fine. Just don't use recursion in Java for these kind of stuff in real world apps ;)
PS. Aside what I just said, I'd choose "" as the base case of my recursive function:
public String reverseString(String s){
if (s.length() == 0)
return s;
return reverseString(s.substring(1)) + s.charAt(0);
}
If you're going to do this, you want to operate on a character array, because a String is immutable and you're going to be copying Strings all over the place if you do it that way.
This is untested and totally stream of consciousness. It probably has an OB1 somewhere. And very not-Java.
public String reverseString(String s)
{
char[] cstr = s.getChars();
reverseCStr(cstr, 0, s.length - 1);
return new String(cstr);
}
/**
* Reverse a character array in place.
*/
private void reverseCStr(char[] a, int s, int e)
{
// This is the middle of the array; we're done.
if (e - s <= 0)
return;
char t = a[s];
a[s] = a[e];
a[e] = t;
reverseCStr(a, s + 1, e - 1);
}
StringBuilder have a reverse method.
System.out.print("REVERSE WORD: " + new StringBuilder(word).reverse().toString());
Or if you don't want to use inbuilt methods
String result= "";
for(int i=word.length(); i>0; i--) {
result+= word.charAt(i-1);
}
System.out.print("REVERSE WORD: " + result);
public class test {
public static void main(String[] args) {
String word = "Stack Overflow";
char[] wordArray = word.toCharArray();
System.out.println("NORMAL WORD=" + Arrays.toString(wordArray));
test.reverse(wordArray);
System.out.println("REVERSE WORD=" + Arrays.toString(wordArray));
}
public static void reverse(char[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
char tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
}
Output:
NORMAL WORD=[S, t, a, c, k, , O, v, e, r, f, l, o, w]
REVERSE WORD=[w, o, l, f, r, e, v, O, , k, c, a, t, S]