Hi, before posting this thread I tried to find solution inside daniweb but was not successful. I found one but it was so specific for one's own homework problem. I always get lost when I do recursion. I read books but most of them start with Fibonacci or Hannoi problem then Good luck! No books talks little details about how recusion in a for loop or while loop works. I will be really greatful if you guys give some time to give me some very simple example about recusion in a for loop works. Please comment code or write down few words.

Thanks.

Recommended Answers

All 8 Replies

Usually one uses recursion instead of a for() loop. Give an example of a problem you are trying to resolve.

the code is here:

import java.util.Scanner;
class Anagram {
    static int size;
    static int count;
    static char[] arrChar = new char[100];
    static Scanner in = new Scanner(System.in);

    public static void main(String[] args) {
        System.out.print("Enter a word: ");    // get word
        String input = in.next();
        size = input.length();                 // find its size
        count = 0;
        for (int j = 0; j < size; j++) // put it in array
        {
            arrChar[j] = input.charAt(j);
        }
        doAnagram(size);                       // anagram it
    }  // end main()
    //-----------------------------------------------------------

    public static void doAnagram(int newSize) {
        if (newSize == 1) // if too small,     
            return;                           // go no further

        for (int j = 0; j < newSize; j++) // for each position,
        {
            doAnagram(newSize - 1);             // anagram remaining
            if (newSize == 2) // if innermost,
            {
                displayWord();                 // display it
            }
            rotate(newSize);                  // rotate word
        }
    }
    //-----------------------------------------------------------
    // rotate left all chars from position to end

    public static void rotate(int newSize) {
        int j;
        int position = size - newSize;
        char temp = arrChar[position];       // save first letter
        for (j = position + 1; j < size; j++) // shift others left
        {
            arrChar[j - 1] = arrChar[j];
        }
        arrChar[j - 1] = temp;                 // put first on right
    }
    //-----------------------------------------------------------
    public static void displayWord() {
        System.out.print(++count + " ");
        for (int j = 0; j < size; j++) {
            System.out.print(arrChar[j]);
        }
        System.out.print("   ");       
        if (count % 6 == 0) {
            System.out.println("");
        }
    }

}  // end class Anagram

My question is why rotate (4) not trigger first. It triggers 2 then 3 then 4

You just need to get a pencil and paper and work through the order of execution. Something like...

doAnagram(4) calls doAnagram(3) (line 27) before it can call rotate (line 32)
doAnagram(3) calls doAnagram(2) (line 27) before it can call rotate (line 32)
doAnagram(2) calls doAnagram(1) (line 27) before it can call rotate (line 32)
doAnagram(1) immediately returns to doAnagram(2)
doAnagram(2) continues execution and arrives at rotate (line 32)
doAnagram(2) completes and returns to doAnagram(3)
doAnagram(3) continues execution and arrives at rotate (line 32)
doAnagram(3) completes and returns to doAnagram(4)
doAnagram(4) continues execution and arrives at rotate (line 32)

commented: Well explained! +15

thanks for replying James. I always loose the stack sequence.. I don't really know how to improve this

I always loose the stack sequence.. I don't really know how to improve this

There's no magic answer. Just step through the code one line at a time and keep a note of what gets called and when. That's what I do (see above for example!)
J

can any one give me some link on recursion so that I can practice more?

Thanks brother

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.