-1

I have a array of strings say {"ABCD","EFGH", "IJKL", "MNOP"} I'm reversing the array as well as individual string within the array and I expect the output to be array of strings but the output obtained in string . Below is my code

public class ReverseString {
    String s = "";

    public static void main(String[] args) {
        ReverseString rs = new ReverseString();
        String value = "";
        String arr[] = { "ABCD", "EFGH", "IJKL", "MNOP" };
        for (int i = arr.length - 1; i >= 0; i--) {
            value = rs.reverseArray(arr[i]);
        }
        System.out.println(value);
    }

    public String reverseArray(String arr1) {
        for (int k = arr1.length() - 1; k >= 0; k--) {
            s += arr1.charAt(k);
        }
        return s.toString();
    }
}

and output is PONMLKJIHGFEDCBA.

How to convert it to array again ?

brso05
  • 13,142
  • 2
  • 21
  • 40
GoodToLearn
  • 41
  • 1
  • 5
  • [string to string array conversion in java](http://stackoverflow.com/a/3413712/6776262) Although, why isn't `value` a String[]? – quackenator May 10 '17 at 20:07
  • It wont be an array since value is a String. Also you use variable s that is concatenating all strings that reverseArray functions works on... – Tomasz Bawor May 10 '17 at 20:08
  • possible duplicate of http://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java (reversing array) and http://stackoverflow.com/questions/7569335/reverse-a-string-in-java (reversing String). If you combine information from both questions you get the solution. – Lukasz_Plawny May 10 '17 at 21:41
  • Possible duplicate of [How do I reverse an int array in Java?](http://stackoverflow.com/questions/2137755/how-do-i-reverse-an-int-array-in-java) – Lukasz_Plawny May 10 '17 at 21:43

3 Answers3

1

You're not storing the the return of reverseArray, but you're just printing it. Try instead :

    public static void main(String[] args) {
        ReverseString rs = new ReverseString();
        String arr[] = { "ABCD", "EFGH", "IJKL", "MNOP" };
        String arr2[] = new String[arr.length];
        int x = 0:
        for (int i = arr.length - 1; i >= 0; i--) {
            arr2[x++] = rs.reverseArray(arr[i]);
        }
        System.out.println(arr2);
    }
ollaw
  • 2,086
  • 1
  • 20
  • 33
0

Try reversing each word individually first then reverse the array. I'll provide a code snip-it in a bit.
Here's the code snip-it. Two methods one to reverse each word.

public void reverseString(String s){
    String result ="";
    for(int i=s.length()-1; i>=0; i--){
        result+=s.charAt(i);
    }
    s = result;
}

public void reversArray(String[] array){
    for(int i = 0; i < array.length / 2; i++)
    {
        String temp = array[i];
        array[i] = array[array.length - i - 1];
        array[array.length - i - 1] = temp;
    }
}
MZ4Code
  • 76
  • 7
0

Here's a simple, full solution that reverses the array in place:

public static void reverse(String[] arr) {
    List<String> wrapper = Arrays.asList(arr);
    wrapper.replaceAll(s -> new StringBuilder(s).reverse().toString());
    Collections.reverse(wrapper);
}
shmosel
  • 49,289
  • 6
  • 73
  • 138