0

I am trying to reverse a full string in java

For example, Good morning so the output should return morning good.

I am tried the below function

public static String reverse(String s)
    {
        if (s.isEmpty())
            return s;
        //int n = 0;
        return reverse(s.substring(1)) + s.charAt(0);

    }

But the above function is converting good morning into gninrom doog. My code working on each character, how I make it work on words. Any hint or guide/explanation will be appreciated.

I already been to this question but not solving my problem

Updated:

Trying the below code with the help of @snr answer and @NathanHughes comments

public static String reverse(String s)
    {
        int s1 = s.indexOf(" ");


        if (s1 != -1) 
        {
         return reverse(s.substring(s1+1)) + s.substring(0,s1);
        }
        else
        {
            return "-1";
        }
    }

But output is

-1good
AHF
  • 1,070
  • 2
  • 15
  • 47
  • 2
    If you knew how to split the string into words, would you be able to solve the problem from there? Have you looked into how to split a string into words? – Scott Hunter Oct 16 '19 at 19:54
  • `s.substring(1)` gives you a string with the first character of `s`. This isn't quite what you want. – Code-Apprentice Oct 16 '19 at 19:55
  • @ScottHunter You mean s.split(" "); in my case? – AHF Oct 16 '19 at 19:56
  • Possible duplicate of [How to reverse words of a Java String](https://stackoverflow.com/questions/15910092/how-to-reverse-words-of-a-java-string) – Tom Oct 16 '19 at 19:56
  • So where are your attempts? Besides copying from the linked question? – RobOhRob Oct 16 '19 at 19:58
  • Dear @ArvindKumarAvinash it helped but I am trying to solve it with recursion – AHF Oct 16 '19 at 19:58
  • Fun fact: breaking a sentence down into words requires full language dictionary. Because of so-called hyphenated compound words, that I even used in writing this very comment! – M. Prokhorov Oct 16 '19 at 20:00
  • 1
    @M. Prokhorov is overthinking, assume space separates words. your hint is: use indexOf and substring. – Nathan Hughes Oct 16 '19 at 20:05
  • @NathanHughes, whether or not I'm overthinking depends on how the end result is going to be used, and what the input is. To some extent, reversing a string is as simple as reversing order of its chars, right? Now, armed with that knowledge, can I reverse a string that contains emoji? Let's not forget that SO is not a site that tries to help very specific user to solve very specific problem. We are supposed to also help future users. – M. Prokhorov Oct 16 '19 at 20:09
  • 1
    @M. Prokhorov: it seems like a toy problem to practice basic recursion, identifying word boundaries is interesting but doesn't seem directly relevant to my interpretation of the problem. obviously if we extend this to CJK charsets then identifying word boundaries gets really tough. – Nathan Hughes Oct 16 '19 at 20:11
  • I am just starter of recursion – AHF Oct 16 '19 at 20:15
  • I am trying to use indexOf(" ") but its return type is int, I can not recall function with it – AHF Oct 16 '19 at 20:15
  • indexOf() returns the beginning index of the substring in the string... or -1 if not found – RobOhRob Oct 16 '19 at 20:21
  • Dear all updated my question. – AHF Oct 16 '19 at 20:31

2 Answers2

6

At first, you need to know where spaces are.

public static String reverse(String str) {
   int space = str.indexOf(" ");
   return space == -1 ? str : reverse(str.substring(space + 1)) + " " + str.substring(0, space);
}
0

This may not be the most efficient way of doing it, but it works:

public static String reverse(String s) {
    String[] wordList;
    wordList = s.split(" ");
    String reverseWordList = "";

    for (int i = 1; i <= wordList.length; i++) {
        reverseWordList += wordList[wordList.length - i] + " ";
    }

    reverseWordList = reverseWordList.trim();
    return reverseWordList;
}
Chad
  • 99
  • 7