userNew
userNew

Reputation: 81

recursively add to strings in java

I am trying to convert int 123 to String "CBA" in java

I know I have some something misunderstood in recursion

After many println statements, I know that at a point

ss is going to be "CBA"

but then it will return "CB" and "C"

I kind of know recursion will work like this, but I don't know how to fix it

Can anyone help me with the code, thanks!

public String recur ( int x, String ss )
{
    if ( x >= 1 && x < 10 )
        ss = ss + "A";

    if ( x >= 10 && x < 100 )
    {
        ss = ss + "B";
        recur( x % 10, ss);
    }

    if ( x >= 100 && x < 1000 )
    {
        ss = ss + "C";
        recur( x % 100, ss);    
    }
return ss;          

Upvotes: 2

Views: 4271

Answers (3)

&#211;scar L&#243;pez
&#211;scar L&#243;pez

Reputation: 236114

Your approach is not correct, for starters, you don't even need to pass a string as a parameter - just build the string in the return values. Also, the logic for converting from integers to strings is overly complicated and only works if the integer contains the digits 1, 2, 3 in that exact order! Here's a simpler, working approach that processes each character in turn and works for integers with any digits:

public String recur(int x) {
    if (x <= 0)
        return "";
    return (char) (x % 10 + 'A' - 1) + recur(x / 10);
}

Explanation:

  • The base case occurs when the integer is equal or less than zero, and we must return an empty string in such case
  • The recursive step works as follows:
    • We obtain the current digit from the integer by taking the remainder: x % 10, and converting the digit to an uppercase character by adding 'A' and subtracting 1
    • We concatenate the previous result with the result of advancing the recursion, dividing the integer by 10

Upvotes: 0

Stephen C
Stephen C

Reputation: 719281

(This Answer is largely for the peanut gallery ...)

Here's my take on the "best" recursive solution to the problem as stated:

public String recur ( int x )
{
    if ( x >= 1 && x < 10 ) {
        return "A";
    } else if ( x >= 10 && x < 100 ) {
        return "B" + recur( x );
    } else if ( x >= 100 && x < 1000 ) {
        return "C" + recur( x );    
    } else {
        throw new IllegalArgumentException();
    }
}

System.err.println(recur(123));

or a bit more generally:

public String recur ( int x)
    return recur0( x, 0 );
}

public String recur0 ( int x, int i )
{
    if ( x <= 0 ) {
        return "";
    else {
        return recur0( x % 10, i + 1 ) + ((char) ('A' + i));
    } 
}

System.err.println(recur(123));

As you can see, no StringBuilder is needed. At best, a StringBuilder is a micro-optimization. It does not make the code any safer, or any more readable. To illustrate, here's the first version redone using a StringBuilder

public void recur ( int x, StringBuilder sb )
{
    if ( x >= 1 && x < 10 ) {
        sb.append("A");
    } else if ( x >= 10 && x < 100 ) {
        sb.append("B");
        recur( x, sb );
    } else if ( x >= 100 && x < 1000 ) {
        sb.append("C");
        recur( x, sb );    
    } 
}

StringBuilder sb = new StringBuilder();
recur(123, sb);
System.err.println(sb.toString());

But the simplest solution for the problem as originally stated is this:

public String nonRecur(int x) {
    if ( x >= 1 && x < 10 ) {
        return "A";
    } else if ( x >= 10 && x < 100 ) {
        return "BA";
    } else if ( x >= 100 && x < 1000 ) {
        return "CBA";    
    } else {
        throw new IllegalArgumentException();
    }
}

System.err.println(nonRecur(123));

Note that there is considerable ambiguity in the way that the problem was stated, and @ÓscarLópez's solution is based on a different interpretation of the problem than mine. He is mapping the digits of the numbers to letters, rather than using the letters to signify magnitude.

In fact, the OP's actual problem is neither of these interpretations, and I don't think that any of the Answers will actually help him ... even though they do answer his Question.

Upvotes: 0

FDinoff
FDinoff

Reputation: 31439

You are ignoring the return value of recur so only the first level of recursion is being seen at the ouptput.

So you could do this to take into account the return value. (Just assign the output of recur to ss)

ss = recur( x % 10, ss);

and the other call.

ss = recur( x % 100, ss);

Upvotes: 1

Related Questions