Sponsored Content
Top Forums Programming Recursion function (Anagramming word) Post 302648497 by rink on Wednesday 30th of May 2012 03:10:13 AM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
american-english-huge(5)					   Users' Manual					  american-english-huge(5)

NAME
american-english-huge - a list of English words DESCRIPTION
/usr/share/dict/american-english-huge is an ASCII file which contains an alphabetic list of words, one per line. FILES
There may be any number of word lists in /usr/share/dict/. /etc/dictionaries-common/words is a symbolic link to the currently-chosen /usr/share/dict/<language> file. /usr/share/dict/words is a symbolic link to /etc/dictionaries-common/words, and is the name by which other software should refer to the system word list. See select-default-wordlist(8) for more information, and/or to change the currently- chosen word list. The directory /usr/share/dict can contain word lists for many languages, with name of the language in English, e.g., /usr/share/dict/french and /usr/share/dict/danish contain respectively lists of French and Danish words if they exist. Such lists should be coded using the ISO 8859-1 character set encoding. SEE ALSO
ispell(1), select-default-wordlist(8), and the Filesystem Hierarchy Standard. HISTORY
The words lists are not specific, and may be generated from any number of sources. The system word list used to be /usr/dict/words. For compatibility, software should check that location if /usr/share/dict/words does not exist. AUTHOR
Word lists are collected and maintained by various authors. The Debian English word lists are built from the SCOWL (Spell- Checker Ori- ented Word Lists) package, whose upstream editor is Kevin Atkinson <kevina@users.sourceforge.net>. Debian 16 June 2003 american-english-huge(5)
All times are GMT -4. The time now is 10:32 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy