TechnoConserve
TechnoConserve

Reputation: 144

Reverse a string using a recursive void method

So I'm trying to write a method that reverses a given string but the catch is that it has to be a void method rather than a return method which is making this difficult. My code seems logical to me but it doesn't work so I'm hoping someone can help me figure out where I'm going wrong.

public class Reverser {

public String text, revText;

/**
 * @param args
 */
public static void main(String[] args) {
    Reverser greeting = new Reverser("Buildings");
    greeting.reverse();
    System.out.println(greeting.getText());

}

public Reverser(String _text){
    text = _text;
}

public void reverse(){
    int len = text.length();
    if(len >= 1){
        String last = text.substring(text.length() - 1, text.length());
        revText += last;
        text = text.substring(0, text.length() - 1);
        Reverser loop = new Reverser(text);     
        loop.reverse();         
    }
}

public String getText(){

    return revText; 
}

}

Upvotes: 2

Views: 5439

Answers (4)

creativeAxe
creativeAxe

Reputation: 119

A slighty different version to what Oscar Lopez responded is this

public class Sentence{
    private String sntce, rvrse;
    private int idx;

    public Sentence(String sentence){
        sntce = sentence;
        rvrse = "";
    }

    /**
        A method to reverse a string recursively. 
        @return void. 
    */
    void reverse(){
        if (idx  == sntce.length()){
            sntce = rvrse;
            return;
        }

        rvrse = sntce.charAt(idx) + rvrse;
        idx++;
        reverse();

    }
    /**
        To test reverse gives the appropriate value. 
        @return the value of sntce. 
    */
    public String getText(){
        return sntce;
    }
}

Upvotes: 1

Óscar López
Óscar López

Reputation: 236004

Here's an idea:

public class Reverser {

    private int idx;
    private String text, revText;

    public static void main(String[] args) {
        Reverser greeting = new Reverser("Buildings");
        greeting.reverse();
        System.out.println(greeting.getText());
    }

    public void reverse() {
        if (idx == text.length())
            return;
        revText = text.charAt(idx) + revText;
        idx++;
        reverse();
    }

    public Reverser(String _text) {
        idx = 0;
        text = _text;
        revText = "";
    }

    public String getText() {
        return revText; 
    }

}

The fundamental difference with respect to your answer, is that I'm using an index attribute to keep track of where exactly I am in the recursion. In that way, I don't have to modify the original text attribute.

Upvotes: 1

Erica
Erica

Reputation: 2251

Given that a string is immutable, you cannot change it in situ. If the only requirement is that there be no return value, and it's okay simply to print out the final string (or to place it into a class variable), then this would work fine for any strings of at least one character:

public static void main(String args[])
{
    reverse("", "original string");
}

public static void reverse(String reversed, String original)
{
    if(original.length() <= 1)
    {
        System.out.println(original.charAt(0) + reversed);
        // (or set it into a shared variable)
        return;
    }

    reverse(original.charAt(0) + reversed, original.substring(1));
}

This solution is going for procedural simplicity, not memory efficiency. This produces quite an unpleasant memory footprint, essentially creating two in-memory strings for each character in the original. It is, however, very logically simple.

Of course, if you're just dumping out to console, then you can achieve the same thing using an algorithm that's pretty much the same as one with a return value:

public static void reverse(String original)
{
    if(original.length() < 1) return;

    System.out.print(original.charAt(original.length() - 1));
    reverse(original.substring(0, original.length() - 1));
}

Upvotes: 0

Synesso
Synesso

Reputation: 38978

Here's a version that uses as few instance variables as possible. Unfortunately you need at least one instance variable to hold the final result (result). Otherwise the state is passed into each recursive call.

(PS, is this homework?)

public class RecursiveVoidStringReverser {

    public static void main(String[] args) {
        final RecursiveVoidStringReverser reverser = new RecursiveVoidStringReverser();
        reverser.reverse("Welcome to the jungle!");
        System.out.println("reverser.result = " + reverser.result());
    }

    private String result;

    public void reverse(String s) {
        if ("".equals(s)) {
            result = s;
        } else {
            reverse(s.toCharArray(), 0);
        }
    }

    private void reverse(char[] chars, int index) {
        if (index > chars.length / 2) {
            result = new String(chars);
        } else {
            char t = chars[index];
            chars[index] = chars[chars.length - index - 1];
            chars[chars.length - index - 1] = t;
            reverse(chars, index+1);
        }
    }

    public String result() {
        return result;
    }
}

Upvotes: 0

Related Questions