-5

I tried to make a method to reverse an array of strings and return a string array with all the words in reverse order. However my code does not work.

public static String[] reverse(String input) {
    String[] reversed = new String[input.length()];

    for (int i = 0; i < input.length(); i++){
        StringBuilder sb = new StringBuilder(input[i]);
        reversed[i] = sb.reverse().toString();
    }
    return reversed;
}

The input in StringBuilder line shows error "array type expected; found: 'java.lang.String'. I cannot figure it out.

The expected results show be like:

String[] reversed = StringReverser.reverse("grey fox jumps over dog");

assertArrayEquals(new String[] {
            "dog","over", "jumps", "fox", "grey"}, reversed);
user572490
  • 57
  • 6
  • Did you search here? This question pops up every couple of weeks, at least. – NomadMaker Jan 24 '21 at 22:55
  • 1
    Does this answer your question? [Reverse a given sentence in Java](https://stackoverflow.com/questions/2713655/reverse-a-given-sentence-in-java) . I don't wanna come off as rude but at the same time it must be said that you must think that a OOP language existing from 1995 must have an answer to a problem as simple as this by now. Try putting a bit more effort next time you search in an engine. In fact this was one of my questions when I did a virtual interview with Microsoft recently so I know for a fact it's a common one. – Omar Abdel Bari Jan 24 '21 at 22:57

2 Answers2

2

There are two problems with your code:

  1. You have passed input[i] to new StringBuilder(...) whereas input is not an array.
  2. You have not reversed the string word-wise at all. There can be many ways to do it. The easiest way is to split the string on whitespace and the store them in an array in the reversed order.

Demo:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        System.out.println(Arrays.toString(reverse("grey fox jumps over dog")));
    }

    public static String[] reverse(String input) {
        String[] words = input.split("\\s+");
        String[] reversed = new String[words.length];

        for (int i = 0; i < words.length; i++) {
            reversed[words.length - i - 1] = words[i];
        }
        return reversed;
    }
}

Output:

[dog, over, jumps, fox, grey]
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
0

There are two problems with your code:

  1. You have passed input[i] to new StringBuilder(...) whereas input is not an array.
  2. You have not reversed the string word-wise at all. There can be many ways to do it. The easiest way is to split the string on whitespace and the store them in an array in the reversed order.
public static String[] reverse(String input) {
    String[] words = input.split("\\s+");

    for (int i = 0, j = words.length - 1; i < j; i++, j--)
        swap(words, i, j);

    return words;
}

private static void swap(String[] arr, int i, int j) {
    String tmp = arr[i];
    arr[i] = arr[j];
    arr[j] = tmp;
}

Demo:

String[] res = reverse("grey fox jumps over dog");
System.out.println(Arrays.toString(res));   // [dog, over, jumps, fox, grey]
Oleg Cherednik
  • 17,377
  • 4
  • 21
  • 35