0

I know how to reverse a string but I was just trying for an alternate way to do the same using a character array. Just tell me why the wrong output comes? a has the String

char[] c = a.toCharArray();
int j = c.length;
char c2[] = c;
for(int i = 0; i < j; i++){
    c2[i] = c[j - i - 1];
}
System.out.println(c2);

like giving harold gives output dloold

1
  • Remember to accept one of the answers as accepted by clicking one of the checkmarks. Welcome to stackoverflow :) Commented May 1, 2018 at 15:20

5 Answers 5

2

You are reading and writing to the same array. This:

char c2[]=c;

is not correct

You want:

   char c2[]= new char[c.length];
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, now I got the reason. Thanks for the help :)
2

There is no real need to use two arrays, and when you do a reference copy you end up working on the same array anyway.

Remember to test your algorithm using an empty array, null, an array with even length and one with odd length.

Here is a single array solution including simple test prints.

public class ReverseTest {
    public static String reverse(String s) {
        if (s == null)
            return null;
        char[] ca = s.toCharArray();
        for (int i = 0; i < ca.length/2; i++) {
            int j = ca.length-i-1;
            char c = ca[i];
            ca[i] = ca[j];
            ca[j] = c; 
        }
        return String.valueOf(ca);
    }

    public static void main(String[] args) {
        System.out.println(reverse(null));
        System.out.println(reverse(""));
        System.out.println(reverse("54321"));
        System.out.println(reverse("4321"));
    }
}

Prints:

null

empty line

12345

1234

Comments

0

This is a reference problem.

char c2[]=c;

This does not copy the array, only its reference. So c2 and c are the same arrays. In your loop you override the first chars with the last ones. Simplest solution is to use:

char c2[]= new char[j];

So you have two arrays and no reference problems.

Alternativly you can swap the array inplace. Loop throught it until the half and swap elements from first and second half.

1 Comment

Yes, now I got the reason. Thanks for the help :)
0

Your assignment char c2[]=c; assigns reference of c to c2. You should create array with the same length with c.

So you should either write;

char c2[] = new char[j];

or

char c2[] = a. toCharArray()

to create array with difference reference in memory.

1 Comment

Yes, now I got the reason. Thanks for the help :)
0

As other answers have mentioned, you must stop halfway through the array.

I'd just like to add that Java has the StringBuilder.reverse() method, which also operates on a character array in place. The following is adapted from the official source code:

private static String reverse(String s) {
    char[] chars = s.toCharArray();
    int length = s.length();
    for (int j = (length-1) / 2; j >= 0; j--) {
        int k = length - j - 1;
        char cj = chars[j];
        char ck = chars[k];
        chars[j] = ck;
        chars[k] = cj;
    }
    return new String(chars);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.