Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Java Question - Reversing user input

  • 09-10-2013 03:26PM
    #1
    Registered Users, Registered Users 2 Posts: 474 ✭✭


    Hi there,
    I am currently attending a Java course in Fás and I am having trouble with a particular question.

    This is the Question:
    "I need to reverse user input, without using StringBuilder".
    We haven't covered it yet and I want to wait for the Instructor to explain it to us.

    I would ask the instructor but a lot of times I am even more confused after!
    If anyone could help me it would be great.

    NOTE: I don't necessarily want the answer, what I would like is someone to explain it to me.

    Thank you very much.


Comments

  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    You can use a for-loop to run through the characters from the end backwards to the beginning and add them to a new string.
    That will reverse the string from eg "ABCDE" to "EDCBA"


  • Registered Users, Registered Users 2 Posts: 710 ✭✭✭mad turnip


    We had this question before in an interview and the girl literly submitted:

    new Stringbuilder(str).reverse()

    I couldn't stop laughing, there was another similar question of sorting a list and in that one she did:

    Collections.sort(list);

    I still can't stop laughing at it. And it was so cheeky she got the job!!!!!!!!!!!!


  • Registered Users, Registered Users 2 Posts: 2,021 ✭✭✭ChRoMe


    mad turnip wrote: »
    We had this question before in an interview and the girl literly submitted:

    new Stringbuilder(str).reverse()

    I couldn't stop laughing, there was another similar question of sorting a list and in that one she did:

    Collections.sort(list);

    I still can't stop laughing at it. And it was so cheeky she got the job!!!!!!!!!!!!

    Why wouldn't she say that, its a completely valid and correct answer. The **** up is on the interviewer not defining the question correctly.


  • Registered Users, Registered Users 2 Posts: 710 ✭✭✭mad turnip


    ChRoMe wrote: »
    Why wouldn't she say that, its a completely valid and correct answer. The **** up is on the interviewer not defining the question correctly.

    I agree completly! One would kind of presume not to do that but still funny!


  • Registered Users, Registered Users 2 Posts: 3,078 ✭✭✭onemorechance


    • Find length of user input string
    • Use this to find last character
    • Minus by one to find second last character
    • Repeat until you reach first character


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,019 ✭✭✭carlmango11


    You can use a for-loop to run through the characters from the end backwards to the beginning and add them to a new string.
    That will reverse the string from eg "ABCDE" to "EDCBA"

    Wouldn't this be inefficient because Strings are immutable?

    Internally the computer would make a whole new string for:

    "E"
    "ED"
    "EDC"
    "EDCD"
    "EDCDA"

    Maybe building a String from a char array of the reversed String would be better


  • Closed Accounts Posts: 8,015 ✭✭✭CreepingDeath


    Maybe building a String from a char array of the reversed String would be better

    Well I didn't want to give the complete solution, just pseudo-code.

    Although using StringBuffer would be better again to build the new string.
    They only stopped them from using StringBuilder because it has a reverse() method in there already.


  • Registered Users, Registered Users 2 Posts: 474 ✭✭Umekichi


    Thanks guys, solved it :D


  • Registered Users, Registered Users 2 Posts: 1,019 ✭✭✭carlmango11


    They only stopped them from using StringBuilder because it has a reverse() method in there already.

    Yeah, stupid though because in the real world StringBuilder would be the way to go. They should really have just said you can't use StringBuilder.reverse


  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,119 Mod ✭✭✭✭Tar.Aldarion


    then they ask you to recursively do it, good times.


  • Advertisement
Advertisement