## liquifiedkronic 4 years ago Can someone please explain to me this code line by line? I really need help for a test tomorrow. My teacher doesn't really teach at all! public class Recursion { public static void OutPut(int pos, String w, String a){ a = a+ " "; int len= w.length();//length of hello is 5 System.out.println(a+w); if(len>1) { OutPut(pos++, w.substring(pos++,len),a); System.out.println(a+w); } } public static void main(String[] args){ OutPut(0,"hello",""); } }

1. liquifiedkronic

it will display hello ello llo lo o lo llo ello hello

2. ecdown

public class Recursion { // defines static method with three inputs // pos = offset into the w string // w = the string to display // a = spaces to make the chars right justified public static void OutPut(int pos, String w, String a){ // This adds an additional space to the a var to // compensate for the substring offset a = a+ " "; // this captures the current length of the w variable // The w variable is updated for each recursive call int len= w.length();//length of hello is 5 //This prints the a spaces followed by the current value of w // This prints the first set of output // hello // ello // llo // lo // o System.out.println(a+w); // If the len of the current substring is greater // than 1 keep calling the recursive function. if(len>1) { // This calls the recurive function OutPut with // the incremented pos value, // the new substring pased on the offset of pos, // and the new set of spaces OutPut(pos++, w.substring(pos++,len),a); // this will end up printing the // lo // llo // ello // hello System.out.println(a+w); } } public static void main(String[] args){ // Call the static method of the Recursion class OutPut // Since it is static you do not need to instantiate the class to call it. OutPut(0,"hello",""); } }

3. ecdown

4. liquifiedkronic

Thank you soo much!

5. ecdown

Oh, and I like your solution better....sorry if my earlier code was unclear...I was working on it at lunch

6. liquifiedkronic

Oh don't worry about it! Any help is appreciated.