Is this recursion?



Hello all,

This is the ole reverse a string classroom assignment.

Recursion is soemthing I never quite understood till now but as I
perceive it, for a method to be considered recursive, it MUST have a
base case and a recursive step.

Fine, with this in mind, I wrote a simple program that reverses a word
that doesn't have a base step but it calls itself. Would this be
considered by the formal defintion of recursion?

To me, I see the base step as implied by doing nothing while you
decrease the parameter by one when you call the method again and THEN
you stop calling it recusively when you get to the last element of the
string. For example, consider the code:

public class StringReversal {
public static void main(String[] args) {

String aWord = "hello";
System.out.print ("\nReversing " + aWord + " to ");

reversal(aWord,aWord.length()-1);

} // end main

/**reverses a string of characters
* @param string is the string passed */
public static void reversal(String aWord,int position){

System.out.print(aWord.substring(position,position+1));

if (position != 0) {
reversal(aWord,position-1);
}
} // end Reversal method
}

I have a feeling the professor is probably not going to like this
because it doesn't fit the 'model' recursion defintion...no base case
and recursive step defined. I'm just wondering what other ppl
think. It's clear to me and it works! :p

Any thoughts appreciated.

.



Relevant Pages

  • Re: Root Program cannot print
    ... zero using recursion ... 'public static void main' method. ... The main method must be static and must have a single 'String ' ...
    (comp.lang.java.programmer)
  • Re: Article on Herbert Schildt, author of C Unleashed, repaired on wikipedia
    ... that deliberately went one beyond the end of the string, ... Within its framework, which sucks, it is a correct program. ... To do this you changed a value parameter to a reference parameter and ... this makes the recursion go wrong. ...
    (comp.programming)
  • Re: Recursion
    ... and output them in reverse order onto another file. ... The point of recursion is that a method calls itself. ... public void reverse(BufferedReader in, BufferedWriter out) ... Your thought to use String is entirely valid. ...
    (comp.lang.java.help)
  • Re: Is this recursion?
    ... This is the ole reverse a string classroom assignment. ... Recursion is soemthing I never quite understood till now but as I perceive it, for a method to be considered recursive, it MUST have a base case and a recursive step. ... I see the base step as implied by doing nothing while you decrease the parameter by one when you call the method again and THEN you stop calling it recusively when you get to the last element of the string. ... public static void reversal{ ...
    (comp.lang.java.help)
  • Re: Recursive functions
    ... recursion is certainly a natural way of defining the ... it IS totally inappropriate to compute ... not mean "inefficient in a typical C implementation". ... If I had an actual requirement to print the string ...
    (comp.lang.c)