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);
}
}
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.
Re: how to reverse the order of words in a sentence?
Fit something much like this into your program
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 :8):
Re: how to reverse the order of words in a sentence?
Quote:
Originally Posted by
Darkzombies
Fit something much like this into your program
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 :8):
\
I GOT IT!!!!!!!!!!!!
THnaks man!!!!!!!!!!!
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.
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] + " ");
}
}
}
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...