Reversing letters of a words in a sentence keeping the words in the same order
Code :
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the word reversing program");
System.out.println("First you will enter a sentence and then the letters of each word will be in the reversed order");
System.out.print("Please enter a Sentence: ");
String original = input.next();
wordReverse(original);
System.out.println("Reversing the words in the sentence " + original + " looks like: " + );
}
public static String wordReverse(String original){
StringTokenizer string = new StringTokenizer(original);
Stack<Character> charStack = new Stack<Character>();
while (string.hasMoreTokens()){
String temp = string.nextToken();
for (int i = 0; i < temp.length(); i ++){
charStack.push(temp.charAt(i));
}
charStack.push(' ');
}
StringBuilder result = new StringBuilder();
while(!charStack.empty()){
result.append(charStack.pop());
}
return result.toString();
}
}
I have two problems. The first problem is with the output. How do you return the string to System.out.println("Reversing the words in the sentence " + original + " looks like: " + );. The second problem is the outcome. When I had the code a little different to see the outcome, the only thing appeared is the first word of the string. I need all the words in the stack to appear.
Re: Reversing letters of a words in a sentence keeping the words in the same order
Quote:
Originally Posted by
georger55
I have two problems. The first problem is with the output. How do you return the string to System.out.println("Reversing the words in the sentence " + original + " looks like: " + );.
You need to assign the String returned by the wordReverse(...) method into a String variable, and then add this variable to the end of your println method call.
Quote:
The second problem is the outcome. When I had the code a little different to see the outcome, the only thing appeared is the first word of the string. I need all the words in the stack to appear.
Can you show us sample output to let us know exactly what you mean here?
Re: Reversing letters of a words in a sentence keeping the words in the same order
Quote:
Originally Posted by
Fubarable
Can you show us sample output to let us know exactly what you mean here?
if a user typed in: java is fun then the output would be avaj si nuf
Re: Reversing letters of a words in a sentence keeping the words in the same order
To get all the words reversed, first you have to read in *all* the words. You only read in one word since you call Scanner#next().
Re: Reversing letters of a words in a sentence keeping the words in the same order
Can you possibly give an example or show me?
Re: Reversing letters of a words in a sentence keeping the words in the same order
In using consoles scanner method you would use nextLine(). EG
Code Java:
Scanner in = new Scanner(System.in);
System.out.println("Go forth and type something.");
String message = in.nextLine();
Re: Reversing letters of a words in a sentence keeping the words in the same order
Exactly as Tjstretch has shown is how to do it, but more importantly, you should learn to refer to the Scanner API to see what methods are available before asking for code handouts here. Studying the API is a necessary skill that you should practice as it will only get better with use.
Re: Reversing letters of a words in a sentence keeping the words in the same order
Fubarable - I am not asking for handouts, I was asking for some help on a part I was stuck on. If it was a hand out I am sure I wouldn't had as much done as I already did. Second, the input.next line reverses the whole sentence. I need to the words to stay in the same order and just have the words reversed.
Tjstretch - Thank you for helping. The only problem with that output it reverses the whole sentence. If you type in hello world the output would be dlrow olleh. I need the output to be olleh dlrow.
Re: Reversing letters of a words in a sentence keeping the words in the same order
Quote:
Originally Posted by
georger55
Fubarable - I am not asking for handouts, I was asking for some help on a part I was stuck on. If it was a hand out I am sure I wouldn't had as much done as I already did. Second, the input.next line reverses the whole sentence. I need to the words to stay in the same order and just have the words reversed.
Tjstretch - Thank you for helping. The only problem with that output it reverses the whole sentence. If you type in hello world the output would be dlrow olleh. I need the output to be olleh dlrow.
I understand. Now that you've got the whole String you need to change your program logic to get the words text reversed but not the words themselves. I'm betting that you can solve this if you try. Why not give it a go first and then show us your attempt.
Re: Reversing letters of a words in a sentence keeping the words in the same order
Hint: use a ArrayList<String> and add each reversed word to it.
Re: Reversing letters of a words in a sentence keeping the words in the same order
Code :
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the word reversing program");
System.out.println("First you will enter a sentence and then the letters of each word will be in the reversed order");
System.out.print("Please enter a Sentence: ");
String original = input.nextLine();
wordReverse(original);
String reverse = wordReverse(original);
fixOrder(reverse);
ArrayList newSentence = fixOrder(reverse);
System.out.println("Reversing the words in the sentence: " + original + ", looks like: " + newSentence );
}
public static String wordReverse(String original){
StringTokenizer string = new StringTokenizer(original);
Stack<Character> charStack = new Stack<Character>();
while (string.hasMoreTokens()){
String stack = string.nextToken();
for (int i = 0; i < stack.length(); i ++){
charStack.push(stack.charAt(i));
}
charStack.push(' ');
}
StringBuilder result = new StringBuilder();
while(!charStack.empty()){
result.append(charStack.pop());
}
return result.toString();
}
public static ArrayList fixOrder(String reverse){
ArrayList arrayList = new ArrayList();
arrayList.add(reverse);
Collections.reverse(arrayList);
return arrayList;
}
}
I tried to reverse the order of the words in this manner but the words were still in the incorrect order.
Re: Reversing letters of a words in a sentence keeping the words in the same order
I would use an ArrayList<String> inside of your wordReverse method. When each word has been reversed, take that char array, create a String from it and put it into the ArrayList.
Re: Reversing letters of a words in a sentence keeping the words in the same order
Hi, I did not use any ArrayList/Stack. Hope this helpful. :)
import java.util.Scanner;
public class ReverseString {
private static Scanner input;
public static void reverse(String s)
{
String s1="";
for (int i=0; i<=s.length()-1; i++)
{
if(s.charAt(i)!=' ')
s1=s1+s.charAt(i);
else {
String ops=revst(s1);
System.out.print(ops+" ");
s1="";
}
if(i==s.length()-1){
String ops=revst(s1);
System.out.print(ops);
}
}
}
public static String revst(String s)
{
String revString = "";
for (int j=s.length()-1; j!=-1; j--)
revString= revString + s.charAt(j);
return revString;
}
public static void main(String[] args)
{
input = new Scanner(System.in);
System.out.println("Please enter a Sentence: ");
String ips = input.nextLine();
System.out.println("Sentence after reversing the words is:");
reverse(ips);
}
}