开发者

Printing A String Vertically Using Recursion In Java

开发者 https://www.devze.com 2022-12-11 18:38 出处:网络
Hey I\'m working on a problem 开发者_JAVA百科where you would print a string vertically using recursion. I know how to do this if i were to use a for loop:

Hey I'm working on a problem 开发者_JAVA百科where you would print a string vertically using recursion. I know how to do this if i were to use a for loop:

for (int i = 0; i < str.length(); i++) {

   System.out.println(str.charAt(i));

but i'm not entirely sure how to do it using recursion. I took care of the base case but i'm not sure how to continue:

if (str == null || str.equals("")) {

    return str;

Any help will be greatly appreciated. Thanks!!!


When in doubt, you can always translate iteration directly into recursion:

for (int i = 0; i < str.length(); i++)
    System.out.println(str.charAt(i));

...becomes:

public void printVertical(String str, int i) {
    if (i < str.length()) {
        System.out.println(str.charAt(i));
        printVertical(str, i + 1);
    }
}

String inputStr = ...
printVertical(inputStr, 0);

Note that there are many ways of doing this that are more elegant. This feels like a homework assignment to me. I would suggest you find one of the subjectively better approaches rather than using my "blind translation" of your loop.


public void printVertString(String str) {
    if (str != null && str.length > 0) //1 base condition
    {
        System.out.println(str.charAt(0)) //2 print first char
        printVertString(str.substring(1)) //3 recursive call, but first char is omitted
    }
}

What this does:

  1. Check that the string is not empty (the base case). If it is, then there shouldn't be any more recursion and the method just returns without doing anything
  2. Print the first character of the string
  3. Calls itself, but only with the second character onwards

So if you send it a str = "CAT", the following is what happens (indents differentiate the different calls of the same function)

1 str is "CAT" --> not empty
2 print "C"
3 call printVertString "AT"
    1 str is "AT" --> not empty
    2 print "A"
    3 call printVertString "T"
        1 str is "T" --> not empty
        2 print "T"
        3 call printVertString ""
            1 str is "" --> EMPTY
            return


The key to understanding recursion is understanding recursion. (bah dum bum)

But seriously, you should think about this in terms of making a smaller problem from the problem that you already have. In this example, you have a string of some length; how could you print part of that string and then have a smaller string with which to repeat the process? That's the essential idea of recursion.

Your base case is correct, so one way you could go about it using the Java libraries and your code above:

public String printVertical(String str)
{
    if (str == null || str.equals(""))
    {
        return str;
    }
    else
    {
        System.out.println(str.charAt(0));
        return printVertical(str.substring(1, str.length);
    }
}


Why would you want to do something like this? This is clearly a case where an iterative process would be clearer and easier to implement than a recursive implementation.

There are a few ways to handle this recursively. Since you are working with Java, I would recommend that you look at the String.substring method in the java core library.


This should do the trick:

void foo (final String str) {
    if (null != str && str.length > 0) {
        System.out.println(str.charAt(0))
        foo(str.substring(1))
    }
}

The point of recursion is that you have a function that calls itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消