Results 1 to 14 of 14
Like Tree1Likes
  • 1 Post By Tolls

Thread: how to reverse word by word?

  1. #1
    yamicaitsith is offline Member
    Join Date
    Sep 2012
    Location
    san marcos,ca
    Posts
    12
    Rep Power
    0

    Default how to reverse word by word?

    how would you reverse a word of a string sentence "hi to all?" without using loops?
    basically we are not using any "for,while,do or if, if else". the only methods we are using is the class strings methods.

    could someone please guide me to the right direction?

    could you provide me the simplest program?

    thank you.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to reverse word by word?

    Quote Originally Posted by yamicaitsith View Post
    how would you reverse a word of a string sentence "hi to all?" without using loops?
    basically we are not using any "for,while,do or if, if else". the only methods we are using is the class strings methods.
    What is your exact instructions, word for word? Can you please post this?

    could someone please guide me to the right direction?
    Let's first find out more about the problem and exactly where you're stuck.

    could you provide me the simplest program?
    No, we should not provide solutions for homework problems as this is unethical and even worse cheats you out of a valuable learning experience. Again, let's find out more about the problem and exactly where you sit, and see if we can help *you* to solve this and come up with the best program/solution.

  3. #3
    yamicaitsith is offline Member
    Join Date
    Sep 2012
    Location
    san marcos,ca
    Posts
    12
    Rep Power
    0

    Default Re: how to reverse word by word?

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    String sentence = "Alienware Lair";
    System.out.println(sentence);

    int n = sentence.length();
    System.out.println(n);
    int position = sentence();

    System.out.println("the word \"Lair\" starts at index" + position);
    n = sentence.substring(0, position + "Alienware");

    String reverse = new StringBuffer("Alienware Lair").reverse().toString();
    String string= "Alienware Lair";

    System.out.println("String before reverse: "+ string);
    System.out.println("String after reverse: "+ reverse);

    }

    }

  4. #4
    yamicaitsith is offline Member
    Join Date
    Sep 2012
    Location
    san marcos,ca
    Posts
    12
    Rep Power
    0

    Default Re: how to reverse word by word?

    im trying to figure out how to change the position from "Alienware Lair" as >>>>>> " Lair Alienware",
    i dont need the solution only what method i should use and if u can provide a example. doesn't has to be like i said the solution?.

    and ill be sincere as it is for a lab job.

    ik very little programming and any help on how to use these methods would be useful.

    thank you.

  5. #5
    yamicaitsith is offline Member
    Join Date
    Sep 2012
    Location
    san marcos,ca
    Posts
    12
    Rep Power
    0

    Default Re: how to reverse word by word?

    String (Java 2 Platform SE v1.4.2)
    ive been looking for hours on the web, but honestly i dont understand most of it.

  6. #6
    yamicaitsith is offline Member
    Join Date
    Sep 2012
    Location
    san marcos,ca
    Posts
    12
    Rep Power
    0

    Default Re: how to reverse word by word?

    i got my chars to reversed and yet the method for changing my sentence hasnt been resolved which is mainly the last thing i need to do

    String sentence = "video games";
    System.out.println(sentence);

    sentence= sentence.toUpperCase();
    System.out.println(sentence);

    int n = sentence.length();
    System.out.println(n);


    String reverse = new StringBuffer("video games").reverse().toString();
    String string= "video games";

    System.out.println("String before reverse: "+ string);
    System.out.println("String after reverse: "+ reverse);

    sentence = sentence.substring(7,10);
    System.out.println(sentence);


    }

    }



    ** i changed the values of this program due to be a lab assignment, its not the original ones i had at the time.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,453
    Rep Power
    20

    Default Re: how to reverse word by word?

    BB Code List - Java Programming Forum

    Please edit your posts and add the tags.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: how to reverse word by word?

    You can use the String method split(" ") to split your String using the spaces as delimiters producing an array of words. But then you'd still need a for loop to iterate backwards through the array. Again, I suggest that you post your actual instructions, as it will be hard for us to help without knowing exactly what your restrictions are.

    Also, as Darryl suggests, please edit your posts above and give your posted code proper code tags so that we may be able to read the code. His links will help.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,165
    Rep Power
    20

    Default Re: how to reverse word by word?

    Recursion down the String[] from a split() call?
    Fubarable likes this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    yamicaitsith is offline Member
    Join Date
    Sep 2012
    Location
    san marcos,ca
    Posts
    12
    Rep Power
    0

    Default Re: how to reverse word by word?

    ok. after analyzing this issue i finally got it solved...

    i will refresh the tags and post my example here for future aid on anyone who might use it for modifying new solutions.
    i want to thank you all moderators for your help :).

    i might be a newbi in java, but hopefully get to known more during my programming and game design career.

    thank u again yami.

  11. #11
    yamicaitsith is offline Member
    Join Date
    Sep 2012
    Location
    san marcos,ca
    Posts
    12
    Rep Power
    0

    Default Re: how to reverse word by word?

    String s1 = "Video";
    String s2 = "games";
    String s3 = s2 + s1;
    s3 = s2 + " " + s1;

    System.out.println(s3);

  12. #12
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: how to reverse word by word?

    Quote Originally Posted by yamicaitsith View Post
    String s1 = "Video";
    String s2 = "games";
    String s3 = s2 + s1;
    s3 = s2 + " " + s1;

    System.out.println(s3);
    I can't decide whether or not you are serious in posting this trivia?

    You should note that in conjunction with recursion, String.indexOf() and String.substring(), this can be used as the basis for a general method for reversing the words of a String. The body of a general method to perform the reverse is just two lines of code.

  13. #13
    yamicaitsith is offline Member
    Join Date
    Sep 2012
    Location
    san marcos,ca
    Posts
    12
    Rep Power
    0

    Default Re: how to reverse word by word?

    Well obviously idk what would you do? If you didn't know about loops and/or was aloud to use em on a project?.
    This matter could be discussed endlessly and I'm sure it could of have better solutions.
    This was what I figured out and worked properly, anything after that I'm sure it can be done depending on what you decide to edit your program.

  14. #14
    sabre150 is offline Member
    Join Date
    Jul 2012
    Location
    Earth
    Posts
    75
    Rep Power
    0

    Default Re: how to reverse word by word?

    Quote Originally Posted by yamicaitsith View Post
    Well obviously idk what would you do? If you didn't know about loops and/or was aloud to use em on a project?.
    This matter could be discussed endlessly and I'm sure it could of have better solutions.
    This was what I figured out and worked properly, anything after that I'm sure it can be done depending on what you decide to edit your program.
    :-) Brilliant! I'm mystified.

Similar Threads

  1. Java stringtokenizer- making it reverse word
    By katiebear128 in forum New To Java
    Replies: 10
    Last Post: 09-29-2013, 05:20 PM
  2. !word
    By walsby14 in forum New To Java
    Replies: 2
    Last Post: 07-16-2012, 05:36 PM
  3. Replies: 3
    Last Post: 08-30-2011, 05:44 PM
  4. Word
    By right2001 in forum New To Java
    Replies: 2
    Last Post: 04-07-2009, 04:25 AM
  5. Word OLE
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-25-2008, 03:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •