Reversing the words order of a string

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












A couple of weeks I had a coding interview and the problem to solve was very simple: reverse the words order of a string. What did I do wrong here?



public static String reverse(String phrase) 
String words = phrase.split(" ");
StringBuffer sb = new StringBuffer();
for (String word : words)
String reversed = new String();
for (int j = word.length() - 1; j >= 0; j--)
reversed += word.charAt(j);

sb = sb.append(reversed).append(" ");


return sb.toString().substring(0, sb.toString().length() - 1);







share|improve this question





















  • String reversed = new String(); this doesn't look good. Why are you doing this when you're already using a StringBuffer (which should be a StringBuilder, by the way)? When is it ever appropriate to use new String()??
    – Hovercraft Full Of Eels
    Jan 3 at 22:32











  • Also, sb = sb.append(... it is unnecessary to assign the StringBuilder reference. Two unforced errors, makes it look like you don't understand objects and references.
    – markspace
    Jan 3 at 22:36










  • In the StringBuffer API: As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
    – Hovercraft Full Of Eels
    Jan 3 at 22:36










  • One more thing: you extract a String from sb (at the return) and then immediately call substring() on that string. Strings are immutable, and this forces another buffer copy. You should have reduced the length of the string builder first, then called toString().
    – markspace
    Jan 3 at 22:40
















up vote
1
down vote

favorite












A couple of weeks I had a coding interview and the problem to solve was very simple: reverse the words order of a string. What did I do wrong here?



public static String reverse(String phrase) 
String words = phrase.split(" ");
StringBuffer sb = new StringBuffer();
for (String word : words)
String reversed = new String();
for (int j = word.length() - 1; j >= 0; j--)
reversed += word.charAt(j);

sb = sb.append(reversed).append(" ");


return sb.toString().substring(0, sb.toString().length() - 1);







share|improve this question





















  • String reversed = new String(); this doesn't look good. Why are you doing this when you're already using a StringBuffer (which should be a StringBuilder, by the way)? When is it ever appropriate to use new String()??
    – Hovercraft Full Of Eels
    Jan 3 at 22:32











  • Also, sb = sb.append(... it is unnecessary to assign the StringBuilder reference. Two unforced errors, makes it look like you don't understand objects and references.
    – markspace
    Jan 3 at 22:36










  • In the StringBuffer API: As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
    – Hovercraft Full Of Eels
    Jan 3 at 22:36










  • One more thing: you extract a String from sb (at the return) and then immediately call substring() on that string. Strings are immutable, and this forces another buffer copy. You should have reduced the length of the string builder first, then called toString().
    – markspace
    Jan 3 at 22:40












up vote
1
down vote

favorite









up vote
1
down vote

favorite











A couple of weeks I had a coding interview and the problem to solve was very simple: reverse the words order of a string. What did I do wrong here?



public static String reverse(String phrase) 
String words = phrase.split(" ");
StringBuffer sb = new StringBuffer();
for (String word : words)
String reversed = new String();
for (int j = word.length() - 1; j >= 0; j--)
reversed += word.charAt(j);

sb = sb.append(reversed).append(" ");


return sb.toString().substring(0, sb.toString().length() - 1);







share|improve this question













A couple of weeks I had a coding interview and the problem to solve was very simple: reverse the words order of a string. What did I do wrong here?



public static String reverse(String phrase) 
String words = phrase.split(" ");
StringBuffer sb = new StringBuffer();
for (String word : words)
String reversed = new String();
for (int j = word.length() - 1; j >= 0; j--)
reversed += word.charAt(j);

sb = sb.append(reversed).append(" ");


return sb.toString().substring(0, sb.toString().length() - 1);









share|improve this question












share|improve this question




share|improve this question








edited Jan 3 at 23:19









Jamal♦

30.1k11114225




30.1k11114225









asked Jan 3 at 22:27









Eliú Abisaí Díaz Vásquez

82




82











  • String reversed = new String(); this doesn't look good. Why are you doing this when you're already using a StringBuffer (which should be a StringBuilder, by the way)? When is it ever appropriate to use new String()??
    – Hovercraft Full Of Eels
    Jan 3 at 22:32











  • Also, sb = sb.append(... it is unnecessary to assign the StringBuilder reference. Two unforced errors, makes it look like you don't understand objects and references.
    – markspace
    Jan 3 at 22:36










  • In the StringBuffer API: As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
    – Hovercraft Full Of Eels
    Jan 3 at 22:36










  • One more thing: you extract a String from sb (at the return) and then immediately call substring() on that string. Strings are immutable, and this forces another buffer copy. You should have reduced the length of the string builder first, then called toString().
    – markspace
    Jan 3 at 22:40
















  • String reversed = new String(); this doesn't look good. Why are you doing this when you're already using a StringBuffer (which should be a StringBuilder, by the way)? When is it ever appropriate to use new String()??
    – Hovercraft Full Of Eels
    Jan 3 at 22:32











  • Also, sb = sb.append(... it is unnecessary to assign the StringBuilder reference. Two unforced errors, makes it look like you don't understand objects and references.
    – markspace
    Jan 3 at 22:36










  • In the StringBuffer API: As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
    – Hovercraft Full Of Eels
    Jan 3 at 22:36










  • One more thing: you extract a String from sb (at the return) and then immediately call substring() on that string. Strings are immutable, and this forces another buffer copy. You should have reduced the length of the string builder first, then called toString().
    – markspace
    Jan 3 at 22:40















String reversed = new String(); this doesn't look good. Why are you doing this when you're already using a StringBuffer (which should be a StringBuilder, by the way)? When is it ever appropriate to use new String()??
– Hovercraft Full Of Eels
Jan 3 at 22:32





String reversed = new String(); this doesn't look good. Why are you doing this when you're already using a StringBuffer (which should be a StringBuilder, by the way)? When is it ever appropriate to use new String()??
– Hovercraft Full Of Eels
Jan 3 at 22:32













Also, sb = sb.append(... it is unnecessary to assign the StringBuilder reference. Two unforced errors, makes it look like you don't understand objects and references.
– markspace
Jan 3 at 22:36




Also, sb = sb.append(... it is unnecessary to assign the StringBuilder reference. Two unforced errors, makes it look like you don't understand objects and references.
– markspace
Jan 3 at 22:36












In the StringBuffer API: As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
– Hovercraft Full Of Eels
Jan 3 at 22:36




In the StringBuffer API: As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread, StringBuilder. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.
– Hovercraft Full Of Eels
Jan 3 at 22:36












One more thing: you extract a String from sb (at the return) and then immediately call substring() on that string. Strings are immutable, and this forces another buffer copy. You should have reduced the length of the string builder first, then called toString().
– markspace
Jan 3 at 22:40




One more thing: you extract a String from sb (at the return) and then immediately call substring() on that string. Strings are immutable, and this forces another buffer copy. You should have reduced the length of the string builder first, then called toString().
– markspace
Jan 3 at 22:40










3 Answers
3






active

oldest

votes

















up vote
0
down vote



accepted










The main reason why you fail may be because your code reverse the entire sentence while only the order of the words must be reversed; Hello the world should be world the hello. If you want to reverse the whole string, there is StringBuilder.html#reverse()



That put aside. Some problems I see is the usage of StringBuffer which is slower but synchronized. There is also this new String() in your loop which is useless because you can already use sb.append. And there is this ugly string building at the end where sb.toString() is abused while sb.delete can do it.




For the fun I made mine which revert the words by traversing the char sequence from the end and insert the character at a given index. The index is changed when a space is found to insert at the end of the new string : https://github.com/gervaisb/stackexchange-codereview/blob/q184229/src/main/java/q184229/Sentence.java



final StringBuilder target = new StringBuilder(value.length());
for (int i=value.length()-1, at=0; i>-1; i--)
char character = value.charAt(i);
if ( Character.isWhitespace(character) )
target.append(character);
at = target.length();
else
target.insert(at, character);







share|improve this answer






























    up vote
    0
    down vote













    Thanks for your fast response guys, after reading your messages I tried to solve it different:



    public static void reverse(String phrase, char delimiter) 
    char newPhrase = new char[phrase.length()];
    int start = 0, i = 0, currentPos = newPhrase.length;
    while (i < phrase.length()) i == phrase.length() - 1)
    char word = phrase.substring(start, i).toCharArray();
    currentPos = currentPos - word.length;
    System.arraycopy(word, 0, newPhrase, currentPos, word.length);
    if (i != phrase.length() - 1)
    newPhrase[--currentPos] = delimiter;

    start = ++i;
    else
    i++;



    System.out.println(new String(newPhrase));






    share|improve this answer





















    • This may be faster, but it's damn hard to read. Dealing with chars is pretty low level and better to be avoided unless really needed. Especially in interviews.
      – maaartinus
      Jan 4 at 1:09










    • Also, if the String contains weird unicode characters, it might be the case that the character from the String does not fit in a char
      – RobAu
      Jan 4 at 11:27










    • @maaartinus indeed I took the main Idea from apache StringUtils.reverse
      – Eliú Abisaí Díaz Vásquez
      Jan 4 at 12:47


















    up vote
    0
    down vote













    Your current solution looks to be too complex. If all you need to do is swap the order of words, Strings that are separated by white space, then why not simply do this:



    • Use String#split("\s+") to split the input text into an array of words -- of Strings split greedily by white-space

    • Iterate through this array backwards in a simple for loop

    • Add these Strings to a StringBuilder (not a StringBuffer which has unnecessary overhead of thread-safety -- we're doing this in only one thread)

    • Avoiding use of new String(...), it's almost never necessary to use this, and there is a down-side to its use, including avoiding appropriate and efficient use of the String-pool.

    • And then return.

    e.g., something as basic as:



    public static String reverseWords(String inText) 
    StringBuilder sb = new StringBuilder();
    String tokens = inText.split("\s+");
    for (int i = tokens.length - 1; i >= 0; i--)
    sb.append(tokens);
    if (i != 0)
    sb.append(" ");


    return sb.toString();






    share|improve this answer





















    • I like your solution but what about String.split method? the interviewer didn't like it and asked me how can I avoid to create several copies of the same string when using String.split and I didn't know what to say
      – Eliú Abisaí Díaz Vásquez
      Jan 3 at 23:11











    • @EliúAbisaíDíazVásquez: "didn't like it" how? why? What specifically were they concerned about, and what metrics are we to use here to measure the quality of an attempt?
      – Hovercraft Full Of Eels
      Jan 3 at 23:14










    • I agreed with you 100% but maybe I am guessing they asked me to solve it different because I was using split instead of iterate throught the array because in their feed back the only mentioned that I didn't solve the problem as expected, but after reading all comments I have more issues like: using StringBuffer and toString
      – Eliú Abisaí Díaz Vásquez
      Jan 3 at 23:22











    • Probably just one of those probing interview questions. "Does the interviewee know a second way of implementing this?" Ignore the /didn't like it/ part and just focus on a different way to parse the input string.
      – markspace
      Jan 4 at 0:27











    • @HovercraftFullOfEels String.split is pretty crazy: github.com/google/guava/wiki/StringsExplained#splitter
      – maaartinus
      Jan 4 at 1:02










    Your Answer




    StackExchange.ifUsing("editor", function ()
    return StackExchange.using("mathjaxEditing", function ()
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    );
    );
    , "mathjax-editing");

    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "196"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );








     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184229%2freversing-the-words-order-of-a-string%23new-answer', 'question_page');

    );

    Post as a guest






























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    The main reason why you fail may be because your code reverse the entire sentence while only the order of the words must be reversed; Hello the world should be world the hello. If you want to reverse the whole string, there is StringBuilder.html#reverse()



    That put aside. Some problems I see is the usage of StringBuffer which is slower but synchronized. There is also this new String() in your loop which is useless because you can already use sb.append. And there is this ugly string building at the end where sb.toString() is abused while sb.delete can do it.




    For the fun I made mine which revert the words by traversing the char sequence from the end and insert the character at a given index. The index is changed when a space is found to insert at the end of the new string : https://github.com/gervaisb/stackexchange-codereview/blob/q184229/src/main/java/q184229/Sentence.java



    final StringBuilder target = new StringBuilder(value.length());
    for (int i=value.length()-1, at=0; i>-1; i--)
    char character = value.charAt(i);
    if ( Character.isWhitespace(character) )
    target.append(character);
    at = target.length();
    else
    target.insert(at, character);







    share|improve this answer



























      up vote
      0
      down vote



      accepted










      The main reason why you fail may be because your code reverse the entire sentence while only the order of the words must be reversed; Hello the world should be world the hello. If you want to reverse the whole string, there is StringBuilder.html#reverse()



      That put aside. Some problems I see is the usage of StringBuffer which is slower but synchronized. There is also this new String() in your loop which is useless because you can already use sb.append. And there is this ugly string building at the end where sb.toString() is abused while sb.delete can do it.




      For the fun I made mine which revert the words by traversing the char sequence from the end and insert the character at a given index. The index is changed when a space is found to insert at the end of the new string : https://github.com/gervaisb/stackexchange-codereview/blob/q184229/src/main/java/q184229/Sentence.java



      final StringBuilder target = new StringBuilder(value.length());
      for (int i=value.length()-1, at=0; i>-1; i--)
      char character = value.charAt(i);
      if ( Character.isWhitespace(character) )
      target.append(character);
      at = target.length();
      else
      target.insert(at, character);







      share|improve this answer

























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        The main reason why you fail may be because your code reverse the entire sentence while only the order of the words must be reversed; Hello the world should be world the hello. If you want to reverse the whole string, there is StringBuilder.html#reverse()



        That put aside. Some problems I see is the usage of StringBuffer which is slower but synchronized. There is also this new String() in your loop which is useless because you can already use sb.append. And there is this ugly string building at the end where sb.toString() is abused while sb.delete can do it.




        For the fun I made mine which revert the words by traversing the char sequence from the end and insert the character at a given index. The index is changed when a space is found to insert at the end of the new string : https://github.com/gervaisb/stackexchange-codereview/blob/q184229/src/main/java/q184229/Sentence.java



        final StringBuilder target = new StringBuilder(value.length());
        for (int i=value.length()-1, at=0; i>-1; i--)
        char character = value.charAt(i);
        if ( Character.isWhitespace(character) )
        target.append(character);
        at = target.length();
        else
        target.insert(at, character);







        share|improve this answer















        The main reason why you fail may be because your code reverse the entire sentence while only the order of the words must be reversed; Hello the world should be world the hello. If you want to reverse the whole string, there is StringBuilder.html#reverse()



        That put aside. Some problems I see is the usage of StringBuffer which is slower but synchronized. There is also this new String() in your loop which is useless because you can already use sb.append. And there is this ugly string building at the end where sb.toString() is abused while sb.delete can do it.




        For the fun I made mine which revert the words by traversing the char sequence from the end and insert the character at a given index. The index is changed when a space is found to insert at the end of the new string : https://github.com/gervaisb/stackexchange-codereview/blob/q184229/src/main/java/q184229/Sentence.java



        final StringBuilder target = new StringBuilder(value.length());
        for (int i=value.length()-1, at=0; i>-1; i--)
        char character = value.charAt(i);
        if ( Character.isWhitespace(character) )
        target.append(character);
        at = target.length();
        else
        target.insert(at, character);








        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited Jan 4 at 13:15


























        answered Jan 4 at 13:04









        gervais.b

        94139




        94139






















            up vote
            0
            down vote













            Thanks for your fast response guys, after reading your messages I tried to solve it different:



            public static void reverse(String phrase, char delimiter) 
            char newPhrase = new char[phrase.length()];
            int start = 0, i = 0, currentPos = newPhrase.length;
            while (i < phrase.length()) i == phrase.length() - 1)
            char word = phrase.substring(start, i).toCharArray();
            currentPos = currentPos - word.length;
            System.arraycopy(word, 0, newPhrase, currentPos, word.length);
            if (i != phrase.length() - 1)
            newPhrase[--currentPos] = delimiter;

            start = ++i;
            else
            i++;



            System.out.println(new String(newPhrase));






            share|improve this answer





















            • This may be faster, but it's damn hard to read. Dealing with chars is pretty low level and better to be avoided unless really needed. Especially in interviews.
              – maaartinus
              Jan 4 at 1:09










            • Also, if the String contains weird unicode characters, it might be the case that the character from the String does not fit in a char
              – RobAu
              Jan 4 at 11:27










            • @maaartinus indeed I took the main Idea from apache StringUtils.reverse
              – Eliú Abisaí Díaz Vásquez
              Jan 4 at 12:47















            up vote
            0
            down vote













            Thanks for your fast response guys, after reading your messages I tried to solve it different:



            public static void reverse(String phrase, char delimiter) 
            char newPhrase = new char[phrase.length()];
            int start = 0, i = 0, currentPos = newPhrase.length;
            while (i < phrase.length()) i == phrase.length() - 1)
            char word = phrase.substring(start, i).toCharArray();
            currentPos = currentPos - word.length;
            System.arraycopy(word, 0, newPhrase, currentPos, word.length);
            if (i != phrase.length() - 1)
            newPhrase[--currentPos] = delimiter;

            start = ++i;
            else
            i++;



            System.out.println(new String(newPhrase));






            share|improve this answer





















            • This may be faster, but it's damn hard to read. Dealing with chars is pretty low level and better to be avoided unless really needed. Especially in interviews.
              – maaartinus
              Jan 4 at 1:09










            • Also, if the String contains weird unicode characters, it might be the case that the character from the String does not fit in a char
              – RobAu
              Jan 4 at 11:27










            • @maaartinus indeed I took the main Idea from apache StringUtils.reverse
              – Eliú Abisaí Díaz Vásquez
              Jan 4 at 12:47













            up vote
            0
            down vote










            up vote
            0
            down vote









            Thanks for your fast response guys, after reading your messages I tried to solve it different:



            public static void reverse(String phrase, char delimiter) 
            char newPhrase = new char[phrase.length()];
            int start = 0, i = 0, currentPos = newPhrase.length;
            while (i < phrase.length()) i == phrase.length() - 1)
            char word = phrase.substring(start, i).toCharArray();
            currentPos = currentPos - word.length;
            System.arraycopy(word, 0, newPhrase, currentPos, word.length);
            if (i != phrase.length() - 1)
            newPhrase[--currentPos] = delimiter;

            start = ++i;
            else
            i++;



            System.out.println(new String(newPhrase));






            share|improve this answer













            Thanks for your fast response guys, after reading your messages I tried to solve it different:



            public static void reverse(String phrase, char delimiter) 
            char newPhrase = new char[phrase.length()];
            int start = 0, i = 0, currentPos = newPhrase.length;
            while (i < phrase.length()) i == phrase.length() - 1)
            char word = phrase.substring(start, i).toCharArray();
            currentPos = currentPos - word.length;
            System.arraycopy(word, 0, newPhrase, currentPos, word.length);
            if (i != phrase.length() - 1)
            newPhrase[--currentPos] = delimiter;

            start = ++i;
            else
            i++;



            System.out.println(new String(newPhrase));







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jan 3 at 22:54









            Eliú Abisaí Díaz Vásquez

            82




            82











            • This may be faster, but it's damn hard to read. Dealing with chars is pretty low level and better to be avoided unless really needed. Especially in interviews.
              – maaartinus
              Jan 4 at 1:09










            • Also, if the String contains weird unicode characters, it might be the case that the character from the String does not fit in a char
              – RobAu
              Jan 4 at 11:27










            • @maaartinus indeed I took the main Idea from apache StringUtils.reverse
              – Eliú Abisaí Díaz Vásquez
              Jan 4 at 12:47

















            • This may be faster, but it's damn hard to read. Dealing with chars is pretty low level and better to be avoided unless really needed. Especially in interviews.
              – maaartinus
              Jan 4 at 1:09










            • Also, if the String contains weird unicode characters, it might be the case that the character from the String does not fit in a char
              – RobAu
              Jan 4 at 11:27










            • @maaartinus indeed I took the main Idea from apache StringUtils.reverse
              – Eliú Abisaí Díaz Vásquez
              Jan 4 at 12:47
















            This may be faster, but it's damn hard to read. Dealing with chars is pretty low level and better to be avoided unless really needed. Especially in interviews.
            – maaartinus
            Jan 4 at 1:09




            This may be faster, but it's damn hard to read. Dealing with chars is pretty low level and better to be avoided unless really needed. Especially in interviews.
            – maaartinus
            Jan 4 at 1:09












            Also, if the String contains weird unicode characters, it might be the case that the character from the String does not fit in a char
            – RobAu
            Jan 4 at 11:27




            Also, if the String contains weird unicode characters, it might be the case that the character from the String does not fit in a char
            – RobAu
            Jan 4 at 11:27












            @maaartinus indeed I took the main Idea from apache StringUtils.reverse
            – Eliú Abisaí Díaz Vásquez
            Jan 4 at 12:47





            @maaartinus indeed I took the main Idea from apache StringUtils.reverse
            – Eliú Abisaí Díaz Vásquez
            Jan 4 at 12:47











            up vote
            0
            down vote













            Your current solution looks to be too complex. If all you need to do is swap the order of words, Strings that are separated by white space, then why not simply do this:



            • Use String#split("\s+") to split the input text into an array of words -- of Strings split greedily by white-space

            • Iterate through this array backwards in a simple for loop

            • Add these Strings to a StringBuilder (not a StringBuffer which has unnecessary overhead of thread-safety -- we're doing this in only one thread)

            • Avoiding use of new String(...), it's almost never necessary to use this, and there is a down-side to its use, including avoiding appropriate and efficient use of the String-pool.

            • And then return.

            e.g., something as basic as:



            public static String reverseWords(String inText) 
            StringBuilder sb = new StringBuilder();
            String tokens = inText.split("\s+");
            for (int i = tokens.length - 1; i >= 0; i--)
            sb.append(tokens);
            if (i != 0)
            sb.append(" ");


            return sb.toString();






            share|improve this answer





















            • I like your solution but what about String.split method? the interviewer didn't like it and asked me how can I avoid to create several copies of the same string when using String.split and I didn't know what to say
              – Eliú Abisaí Díaz Vásquez
              Jan 3 at 23:11











            • @EliúAbisaíDíazVásquez: "didn't like it" how? why? What specifically were they concerned about, and what metrics are we to use here to measure the quality of an attempt?
              – Hovercraft Full Of Eels
              Jan 3 at 23:14










            • I agreed with you 100% but maybe I am guessing they asked me to solve it different because I was using split instead of iterate throught the array because in their feed back the only mentioned that I didn't solve the problem as expected, but after reading all comments I have more issues like: using StringBuffer and toString
              – Eliú Abisaí Díaz Vásquez
              Jan 3 at 23:22











            • Probably just one of those probing interview questions. "Does the interviewee know a second way of implementing this?" Ignore the /didn't like it/ part and just focus on a different way to parse the input string.
              – markspace
              Jan 4 at 0:27











            • @HovercraftFullOfEels String.split is pretty crazy: github.com/google/guava/wiki/StringsExplained#splitter
              – maaartinus
              Jan 4 at 1:02














            up vote
            0
            down vote













            Your current solution looks to be too complex. If all you need to do is swap the order of words, Strings that are separated by white space, then why not simply do this:



            • Use String#split("\s+") to split the input text into an array of words -- of Strings split greedily by white-space

            • Iterate through this array backwards in a simple for loop

            • Add these Strings to a StringBuilder (not a StringBuffer which has unnecessary overhead of thread-safety -- we're doing this in only one thread)

            • Avoiding use of new String(...), it's almost never necessary to use this, and there is a down-side to its use, including avoiding appropriate and efficient use of the String-pool.

            • And then return.

            e.g., something as basic as:



            public static String reverseWords(String inText) 
            StringBuilder sb = new StringBuilder();
            String tokens = inText.split("\s+");
            for (int i = tokens.length - 1; i >= 0; i--)
            sb.append(tokens);
            if (i != 0)
            sb.append(" ");


            return sb.toString();






            share|improve this answer





















            • I like your solution but what about String.split method? the interviewer didn't like it and asked me how can I avoid to create several copies of the same string when using String.split and I didn't know what to say
              – Eliú Abisaí Díaz Vásquez
              Jan 3 at 23:11











            • @EliúAbisaíDíazVásquez: "didn't like it" how? why? What specifically were they concerned about, and what metrics are we to use here to measure the quality of an attempt?
              – Hovercraft Full Of Eels
              Jan 3 at 23:14










            • I agreed with you 100% but maybe I am guessing they asked me to solve it different because I was using split instead of iterate throught the array because in their feed back the only mentioned that I didn't solve the problem as expected, but after reading all comments I have more issues like: using StringBuffer and toString
              – Eliú Abisaí Díaz Vásquez
              Jan 3 at 23:22











            • Probably just one of those probing interview questions. "Does the interviewee know a second way of implementing this?" Ignore the /didn't like it/ part and just focus on a different way to parse the input string.
              – markspace
              Jan 4 at 0:27











            • @HovercraftFullOfEels String.split is pretty crazy: github.com/google/guava/wiki/StringsExplained#splitter
              – maaartinus
              Jan 4 at 1:02












            up vote
            0
            down vote










            up vote
            0
            down vote









            Your current solution looks to be too complex. If all you need to do is swap the order of words, Strings that are separated by white space, then why not simply do this:



            • Use String#split("\s+") to split the input text into an array of words -- of Strings split greedily by white-space

            • Iterate through this array backwards in a simple for loop

            • Add these Strings to a StringBuilder (not a StringBuffer which has unnecessary overhead of thread-safety -- we're doing this in only one thread)

            • Avoiding use of new String(...), it's almost never necessary to use this, and there is a down-side to its use, including avoiding appropriate and efficient use of the String-pool.

            • And then return.

            e.g., something as basic as:



            public static String reverseWords(String inText) 
            StringBuilder sb = new StringBuilder();
            String tokens = inText.split("\s+");
            for (int i = tokens.length - 1; i >= 0; i--)
            sb.append(tokens);
            if (i != 0)
            sb.append(" ");


            return sb.toString();






            share|improve this answer













            Your current solution looks to be too complex. If all you need to do is swap the order of words, Strings that are separated by white space, then why not simply do this:



            • Use String#split("\s+") to split the input text into an array of words -- of Strings split greedily by white-space

            • Iterate through this array backwards in a simple for loop

            • Add these Strings to a StringBuilder (not a StringBuffer which has unnecessary overhead of thread-safety -- we're doing this in only one thread)

            • Avoiding use of new String(...), it's almost never necessary to use this, and there is a down-side to its use, including avoiding appropriate and efficient use of the String-pool.

            • And then return.

            e.g., something as basic as:



            public static String reverseWords(String inText) 
            StringBuilder sb = new StringBuilder();
            String tokens = inText.split("\s+");
            for (int i = tokens.length - 1; i >= 0; i--)
            sb.append(tokens);
            if (i != 0)
            sb.append(" ");


            return sb.toString();







            share|improve this answer













            share|improve this answer



            share|improve this answer











            answered Jan 3 at 23:04









            Hovercraft Full Of Eels

            661410




            661410











            • I like your solution but what about String.split method? the interviewer didn't like it and asked me how can I avoid to create several copies of the same string when using String.split and I didn't know what to say
              – Eliú Abisaí Díaz Vásquez
              Jan 3 at 23:11











            • @EliúAbisaíDíazVásquez: "didn't like it" how? why? What specifically were they concerned about, and what metrics are we to use here to measure the quality of an attempt?
              – Hovercraft Full Of Eels
              Jan 3 at 23:14










            • I agreed with you 100% but maybe I am guessing they asked me to solve it different because I was using split instead of iterate throught the array because in their feed back the only mentioned that I didn't solve the problem as expected, but after reading all comments I have more issues like: using StringBuffer and toString
              – Eliú Abisaí Díaz Vásquez
              Jan 3 at 23:22











            • Probably just one of those probing interview questions. "Does the interviewee know a second way of implementing this?" Ignore the /didn't like it/ part and just focus on a different way to parse the input string.
              – markspace
              Jan 4 at 0:27











            • @HovercraftFullOfEels String.split is pretty crazy: github.com/google/guava/wiki/StringsExplained#splitter
              – maaartinus
              Jan 4 at 1:02
















            • I like your solution but what about String.split method? the interviewer didn't like it and asked me how can I avoid to create several copies of the same string when using String.split and I didn't know what to say
              – Eliú Abisaí Díaz Vásquez
              Jan 3 at 23:11











            • @EliúAbisaíDíazVásquez: "didn't like it" how? why? What specifically were they concerned about, and what metrics are we to use here to measure the quality of an attempt?
              – Hovercraft Full Of Eels
              Jan 3 at 23:14










            • I agreed with you 100% but maybe I am guessing they asked me to solve it different because I was using split instead of iterate throught the array because in their feed back the only mentioned that I didn't solve the problem as expected, but after reading all comments I have more issues like: using StringBuffer and toString
              – Eliú Abisaí Díaz Vásquez
              Jan 3 at 23:22











            • Probably just one of those probing interview questions. "Does the interviewee know a second way of implementing this?" Ignore the /didn't like it/ part and just focus on a different way to parse the input string.
              – markspace
              Jan 4 at 0:27











            • @HovercraftFullOfEels String.split is pretty crazy: github.com/google/guava/wiki/StringsExplained#splitter
              – maaartinus
              Jan 4 at 1:02















            I like your solution but what about String.split method? the interviewer didn't like it and asked me how can I avoid to create several copies of the same string when using String.split and I didn't know what to say
            – Eliú Abisaí Díaz Vásquez
            Jan 3 at 23:11





            I like your solution but what about String.split method? the interviewer didn't like it and asked me how can I avoid to create several copies of the same string when using String.split and I didn't know what to say
            – Eliú Abisaí Díaz Vásquez
            Jan 3 at 23:11













            @EliúAbisaíDíazVásquez: "didn't like it" how? why? What specifically were they concerned about, and what metrics are we to use here to measure the quality of an attempt?
            – Hovercraft Full Of Eels
            Jan 3 at 23:14




            @EliúAbisaíDíazVásquez: "didn't like it" how? why? What specifically were they concerned about, and what metrics are we to use here to measure the quality of an attempt?
            – Hovercraft Full Of Eels
            Jan 3 at 23:14












            I agreed with you 100% but maybe I am guessing they asked me to solve it different because I was using split instead of iterate throught the array because in their feed back the only mentioned that I didn't solve the problem as expected, but after reading all comments I have more issues like: using StringBuffer and toString
            – Eliú Abisaí Díaz Vásquez
            Jan 3 at 23:22





            I agreed with you 100% but maybe I am guessing they asked me to solve it different because I was using split instead of iterate throught the array because in their feed back the only mentioned that I didn't solve the problem as expected, but after reading all comments I have more issues like: using StringBuffer and toString
            – Eliú Abisaí Díaz Vásquez
            Jan 3 at 23:22













            Probably just one of those probing interview questions. "Does the interviewee know a second way of implementing this?" Ignore the /didn't like it/ part and just focus on a different way to parse the input string.
            – markspace
            Jan 4 at 0:27





            Probably just one of those probing interview questions. "Does the interviewee know a second way of implementing this?" Ignore the /didn't like it/ part and just focus on a different way to parse the input string.
            – markspace
            Jan 4 at 0:27













            @HovercraftFullOfEels String.split is pretty crazy: github.com/google/guava/wiki/StringsExplained#splitter
            – maaartinus
            Jan 4 at 1:02




            @HovercraftFullOfEels String.split is pretty crazy: github.com/google/guava/wiki/StringsExplained#splitter
            – maaartinus
            Jan 4 at 1:02












             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184229%2freversing-the-words-order-of-a-string%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Greedy Best First Search implementation in Rust

            Function to Return a JSON Like Objects Using VBA Collections and Arrays

            C++11 CLH Lock Implementation