Results 1 to 7 of 7
  1. #1
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default how to reverse the order of words in a sentence?

    Hi.
    I want to write an application that inputs a sentence, tokenizes the words with method split and reverse the words (not the letters)...I am stuck at the last part: how to reverse them...should I use the method reverse(); ?
    Here is my code....please help!

    Thanks!


    import java.util.Scanner;
    import java.util.StringTokenizer;


    public class ReversedWords
    {
    //execute application
    public static void main( String [] args)
    {
    //get sentence
    Scanner scanner = new Scanner ( System.in);
    System.out.println ( "Enter a sentence and press Enter" );
    String sentence = scanner.nextLine();


    //process user sentence
    String[] words = sentence.split ( " " );
    System.out.println ( " The reversed words are : ");

    for (String word : words)
    System.out.println (word);
    }
    }

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: how to reverse the order of words in a sentence?

    You can create a simple reverse() method to reverse the array you've got from calling the split() method.

    1. Start by creating another array of the same size.
    2. Create a for loop to iterate the words array from the end to the beginning.
    3. Read each word from the first array and place it in your new array from the first index to the last.
    4. Then you will have this new array in a reverse order of the first one.

    If you don't want to use a second array then you can iterate half-size of your array. In the loop swap the first half element with the second half element. The first element with the last element. Then you also have them a reverse order.

    Another option that you can use is using the Stack class. You push each word into the stack and then pop it out. Then you'll get them back in reverse order. You can also simplify it even more using the Collections.reverse() method. If you are allowed to use predefined method. But this method requires you to use a List.
    Last edited by wsaryada; 11-29-2012 at 04:00 AM.

  3. #3
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: how to reverse the order of words in a sentence?

    Fit something much like this into your program

    Java Code:
        private static String[] s = {"Hello", "World"};
        public static void main(String[] args) 
        {
            for(int x = s.length - 1; x > -1; x--){
                System.out.print(s[x]);
            }
        }
    this is just an example of how it's done
    Last edited by Darkzombies; 11-29-2012 at 03:57 AM.

  4. #4
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: how to reverse the order of words in a sentence?

    Quote Originally Posted by Darkzombies View Post
    Fit something much like this into your program

    Java Code:
        private static String[] s = {"Hello", "World"};
        public static void main(String[] args) 
        {
            for(int x = s.length - 1; x > -1; x--){
                System.out.print(s[x]);
            }
        }
    this is just an example of how it's done
    \


    I GOT IT!!!!!!!!!!!!
    THnaks man!!!!!!!!!!!
    Last edited by dksellou; 11-29-2012 at 04:12 AM.

  5. #5
    Darkzombies is offline Senior Member
    Join Date
    Nov 2012
    Posts
    105
    Rep Power
    0

    Default Re: how to reverse the order of words in a sentence?

    I hate giving out code, but if you learn from it, it's ok, study this.

    Java Code:
    import java.util.Scanner;
    import java.util.StringTokenizer;
    
    
    public class ReversedWords
    {
    //execute application
    public static void main( String [] args)
    {
    //get sentence
    Scanner scanner = new Scanner ( System.in);
    System.out.println ("Enter a sentence and press Enter");
    String sentence = scanner.nextLine();
    
    
    //process user sentence
    String[] words = sentence.split (" ");
    System.out.println ( " The reversed words are : ");
    
    for(int x = words.length - 1; x > -1; x--){
    	System.out.print(words[x] + " ");
    	}
    }
    }

  6. #6
    dksellou is offline Member
    Join Date
    Sep 2012
    Posts
    13
    Rep Power
    0

    Default Re: how to reverse the order of words in a sentence?

    As I said, I got it before you sent me the last post. Again, thanks for your help...

  7. #7
    prasune is offline Member
    Join Date
    Sep 2014
    Posts
    1
    Rep Power
    0

    Default Re: how to reverse the order of words in a sentence?

    Quote Originally Posted by dksellou View Post
    Hi.
    I want to write an application that inputs a sentence, tokenizes the words with method split and reverse the words (not the letters)...I am stuck at the last part: how to reverse them...should I use the method reverse(); ?
    Here is my code....please help!

    Thanks!


    import java.util.Scanner;
    import java.util.StringTokenizer;


    public class ReversedWords
    {
    //execute application
    public static void main( String [] args)
    {
    //get sentence
    Scanner scanner = new Scanner ( System.in);
    System.out.println ( "Enter a sentence and press Enter" );
    String sentence = scanner.nextLine();


    //process user sentence
    String[] words = sentence.split ( " " );
    System.out.println ( " The reversed words are : ");

    for (String word : words)
    System.out.println (word);
    }
    }
    You can find solution at [link to crappy solution deleted and old thread closed]
    Last edited by JosAH; 09-06-2014 at 06:01 AM.

Similar Threads

  1. Replies: 3
    Last Post: 07-08-2012, 11:08 PM
  2. Map of words and their occurence [ Reverse Version ]
    By Goldstein in forum New To Java
    Replies: 1
    Last Post: 02-11-2012, 10:51 PM
  3. Reverse words of string.
    By Vikash Kumar Singh in forum Advanced Java
    Replies: 6
    Last Post: 08-07-2011, 12:28 PM
  4. Replies: 0
    Last Post: 02-25-2010, 12:51 PM
  5. Math.random in array and reverse order
    By eugenechia in forum New To Java
    Replies: 4
    Last Post: 02-17-2010, 03:33 AM

Posting Permissions

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