0
class StringRev{
    public static void main(String args[]){
    String str = "He is the one";
    String temp = "";
    String finalString = "";
        for(int i =str.length()-1;i>=0;i--){
            temp +=i!=0?str.charAt(i):str.charAt(i)+" ";
            if(str.charAt(i) == ' '||i==0){
                for(int j=temp.length()-1;j>=0;j--){
                    finalString += temp.charAt(j);
                }
                temp = "";
            }
        }
            System.out.println(finalString);
    }
}

I tried to solve.I have to reverse the string "He is the one" to "one the is He".
I have written some programs in Java but am looking for other best solutions.
Suggest any possible ways to minimize the current.

Himanshu
  • 4,327
  • 16
  • 31
  • 39

3 Answers3

0

Simply just split given string on spaces, will give you an array of words, iterate reversely and append it to new string as following:

String string = "He is the one";
System.out.println("Original String: " + string);

String[] arr = string.split(" ");

String reverseString = "";
for(int i = arr.length - 1; i >= 0; i--){
    reverseString += arr[i] + " ";
}

System.out.println("Reverse String: " + reverseString);

OR you can use Arrays utility to convert split-ed string into List, make it reverse it using Collection utility reverse method, iterate over it and create string as following:

List<String> list = Arrays.asList(string.split(" "));
Collections.reverse(list);

String reserveStringUsingList = "";
for(String str : list){
    reserveStringUsingList += str + " ";
}

System.out.println("Reverse String using List: " + reserveStringUsingList);
Parkash Kumar
  • 4,710
  • 3
  • 23
  • 39
  • And now you even try to steal things from my answer and present as your own? Pathetic. – Dropout Dec 15 '15 at 07:43
  • We all have been steeling from JAVA. I just improved my answer. While, there was no need to add detail about trim(). – Parkash Kumar Dec 15 '15 at 07:50
  • Yeah, you improved it after reading my answer. Nobody is stealing from Java, looks like you don't understand the meaning of that word. I usually don't mind when people do this, but If you steal and at the same time try to make my answer look like crap, then I'm not going to just silently let it slip, because this attitude is really not welcome on SO. – Dropout Dec 15 '15 at 07:59
  • Ok, I have updated my answer. Don't get arrogant, perhaps you better understand the game of words. – Parkash Kumar Dec 15 '15 at 08:20
0

Split the String into words and the iterate downwards and append the words back to a String.

public class StringRev{
    public static void main(String args[]){
        String input = "He is the one";
        String output = "";
        String[] inputSplit = input.split(" ");

        for(int i=inputSplit.length-1; i>=0; i--){
            output += inputSplit[i] + " ";
        }

        System.out.println(output.trim()); //remove trailing space
    }
}

If you can use Collections and StringUtil then it's even more straight forward.

public class StringRev{
    public static void main(String args[]){
        String input = "He is the one";
        String[] inputSplit = input.split(" ");

        Collections.reverse(Arrays.asList(inputSplit));
        String output = StringUtil.join(Arrays.asList(inputSplit), " ");

        System.out.println(output);
    }
}
Dropout
  • 13,653
  • 10
  • 56
  • 109
  • To use StringUtil or StringUtils class, we need to use additional library. See: http://stackoverflow.com/questions/8660151/how-do-i-use-stringutils-in-java#answer-8660166 – Parkash Kumar Dec 15 '15 at 07:33
  • @ParkashKumar that's kind of obvious isn't it? When you want to use Collections you need `java.util.Collections` and I'm not having a go about it in your answer.. Stop trying to make other people's answers look less good than yours by all means just to get those few points.. – Dropout Dec 15 '15 at 07:42
  • That's not all about the points. Its regarding simple and straightforward answer, which satisfies OP's requirement. While, to use java.util.Collections you will not require any additional library except the JDK / JRE, obviously you will be using for your java program to run. – Parkash Kumar Dec 15 '15 at 07:47
  • "If you can use `Collections` and `StringUtil`" – Dropout Dec 15 '15 at 07:58
  • I really didn't mean to lower your answer. Sorry, if you minded my initial comment. – Parkash Kumar Dec 15 '15 at 08:22
0

Here is the solution you asked for, using ListIterator:

Code:

public static void main(String[] args) {
    final String str = "He is the one";
    System.out.println(str);
    System.out.println(reverseWords(str));
}

static String reverseWords(String str) {
    StringBuilder result = new StringBuilder(str.length());
    List<String> words = Arrays.asList(str.split(" "));
    if(!words.isEmpty()) {
        ListIterator<String> it = words.listIterator(words.size());
        result.append(it.previous());
        while (it.hasPrevious()) {
            result.append(' ').append(it.previous());
        }
    }
    return result.toString();
}

Output:

He is the one
one the is He
Binkan Salaryman
  • 3,008
  • 1
  • 17
  • 29