Recursion function (Anagramming word)


 
Thread Tools Search this Thread
Top Forums Programming Recursion function (Anagramming word)
# 1  
Old 05-30-2012
Recursion function (Anagramming word)

Sorry for my english

Hello all my friends and seniors, i had created a programm in c++
(anagrammig of word) it works fine but i cannot understand how exactly
recursion is working , i mean oh.. first look at the code .

Code:
    #include <iostream>
    #include <string>
    using namespace std;
    ////////////////////////////////////////////////////////////////
    class word
    {
    private:
    int size; //length of input word
    int count; //numbers in display
    string workStr; //workspace
    void rotate(int); //rotate part of workStr
    void displayWord(); //display workStr
    public:
    word(string); //constructor
    void anagram(int); //anagram ourselves
    };
    //--------------------------------------------------------------
    //constructor
    word::word(string inpStr) : workStr(inpStr), count(0)
    { //initialize workStr
    size = inpStr.length(); //number of characters 
    }
    //--------------------------------------------------------------
    void word::anagram(int newSize)
    {
    cout<<"Inside function"<<" "<<newSize<<endl;
    if(newSize == 1) //if too small,
    return; //go no further
    for(int j=0; j<newSize; j++) //for each position, 
    {
    cout<<"Inside loop before recursion"<<endl;
    anagram(newSize-1); //anagram remaining
    cout<<"After recursion function"<<" "<<newSize<<endl;
    if(newSize==2) //if innermost,
    displayWord(); // display it
    rotate(newSize); //rotate word
    cout<<"After rotate function"<<endl;
    }
    }
    //--------------------------------------------------------------
    //rotate left all chars from position to end
    void word::rotate(int newSize) 
    {
    int j;
    int position = size - newSize;
    char temp = workStr[position]; //save first letter
    for(j=position+1; j<size; j++) //shift others left
    workStr[j-1] = workStr[j];
    workStr[j-1] = temp; //put first on right
    }
    //--------------------------------------------------------------
    void word::displayWord()
    {

    cout << workStr << " ";
    cout << endl;
    }
    ////////////////////////////////////////////////////////////////
    int main()
    {
     string input;
     int length;
     cout << "Enter a word: "; //get word
     cin >> input;
     length = input.length(); //get its length
     word theWord(input); //make a word object
     theWord.anagram(length); //anagram it
     return 0;
     } //end main()

This programm running fine but in function anagram, i mean inside the loop
The size of newSize variable, how can be "2" after recursion function ,
i mean in this statement

"cout<<"After recursion function"<<" "<<newSize<<endl;"

This statement is in the anagram function . This Line is my problem how
newSize variable become 2 after recursion i cannot understand any help
# 2  
Old 05-31-2012
Code:
anagram(newSize-1);

You're calling anagram() from inside anagram() with one smaller size. That's how it gets smaller.

The variable itself doesn't get changed, as much as when it's called again, the new call is separate because the old call hasn't finished. There's more than one variable of the same name.

Last edited by Corona688; 05-31-2012 at 10:45 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Zsh function does not terminate when ${name:?word} fails

(Simplified example): I have in my .zshrc the following function definition: function foo { local p=${1:?parameter missing} echo continue .... } Running the function with just foo produces, as expected, the message parameter missing, but it also output continue. I had expected... (0 Replies)
Discussion started by: rovf
0 Replies

2. UNIX for Dummies Questions & Answers

Find EXACT word in files, just the word: no prefix, no suffix, no 'similar', just the word

I have a file that has the words I want to find in other files (but lets say I just want to find my words in a single file). Those words are IDs, so if my word is ZZZ4, outputs like aaZZZ4, ZZZ4bb, aaZZZ4bb, ZZ4, ZZZ, ZyZ4, ZZZ4.8 (or anything like that) WON'T BE USEFUL. I need the whole word... (6 Replies)
Discussion started by: chicchan
6 Replies

3. Shell Programming and Scripting

script recursion

Can someone please explain me why the following script calls it self recursively: #!/bin/bash echo Called $0 while this not: #!/bin/bash echo Called $($0) Thanks (6 Replies)
Discussion started by: superpointer
6 Replies

4. Programming

C Recursion (explain)

Hi, Question: how come the output is like that? Can explain to me abit. I am learning C. Thanks! #include <stdio.h> #include <string.h> void printit(char line_of_char, int index); int main() { char line_of_char; int index = -1; strcpy(line_of_char, "This is a string."); ... (5 Replies)
Discussion started by: seede
5 Replies

5. UNIX for Dummies Questions & Answers

How to find function name based on word search

Hi All, I want to find out function name based on word. Here i ll give u one example I have several files based on below format. file will start and ends with same name only EX: file1.txt public function calculate1() { ---- ---- call Global Function1() ---- ---- } END... (9 Replies)
Discussion started by: spc432
9 Replies

6. UNIX for Dummies Questions & Answers

Function Recursion Shift Problem

Hi, I'm trying to create a script that will display the contents of the users directories, but i'm confused about how to incorporate the shift properly. The problem I'm getting with my script is that it goes throught the first couple of directories but then returns an error as it loses the... (10 Replies)
Discussion started by: nuvpal
10 Replies

7. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

8. Shell Programming and Scripting

Help Help Help in recursion

Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me factorial() { if then y=`expr $1 - 1` x=$(( $1 \* factorial $y ))... (6 Replies)
Discussion started by: murtaza
6 Replies

9. Shell Programming and Scripting

recursion too deep

I am running a korn shell script which has a recursive function. The script ran for 117 iterations and ended up with the following error "recursion too deep". what should be done to avert this? Thanks in advance Swamy p.s. I am on UNIX MPRAS V4 (3 Replies)
Discussion started by: swamy455
3 Replies

10. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies
Login or Register to Ask a Question