Java使用堆栈来反转句子中的单词
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21716830/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Using a stack to reverse the words in a sentence
提问by user3071909
I am supposed to write a code that reads a sentence from the user and prints the characters of the words in the sentence backwards. It should include a helper method that takes a String as a parameter and returns a new String with the characters reversed. The individual words are reversed, for example the sentence "Hi dog cat". would print "iH god tac". I can make the entire sentence reverse but i cant figure out how to reverse individual words. Thanks! Also, i know how to return the String once i have found it, but i just cant get the right string
我应该编写一个代码,从用户那里读取一个句子,然后向后打印句子中单词的字符。它应该包含一个辅助方法,该方法将字符串作为参数并返回一个字符颠倒的新字符串。单个单词被颠倒,例如句子“Hi dog cat”。会打印“iH God tac”。我可以将整个句子颠倒,但我无法弄清楚如何颠倒单个单词。谢谢!另外,我知道如何在找到字符串后返回它,但我就是无法获得正确的字符串
import java.util.Scanner;
import java.util.Stack;
public class ReverseStack
{
public static void main(String[] args)
{
String sentence;
System.out.println("Enter a sentence: ");
Scanner scan = new Scanner(System.in);
sentence = scan.nextLine();
String k = PrintStack(sentence);
}
private static String PrintStack(String sentence)
{
String reverse;
String stringReversed = "";
Stack<String> stack= new Stack<String>();
sentence.split(" ");
for(int i=0;i<sentence.length(); i++)
{
stack.push(sentence.substring(i, i+1));
}
while(!stack.isEmpty())
{
stringReversed += stack.pop();
}
System.out.println("Reverse is: " + stringReversed);
return reverse;
}
}
回答by hcps-tenembasj
I will type an expatiation so you can still get the experience of writing the code, rather than me just giving you the code.
我会输入一个解释,这样你仍然可以获得编写代码的经验,而不是我只是给你代码。
First create a Stack
of Character
s. Then use add each character in the String
to the Stack
, starting with the first char
, then the second, and so on. Now either clear the String
or create a new String
to store the reversed word. Finally, add each character from the Stack
to the String
. This will pull the last character off first, then the second to last, and so on.
首先创建一个Stack
的Character
秒。然后使用将 中的每个字符添加String
到 中Stack
,从第一个开始char
,然后是第二个,依此类推。现在清除String
或创建一个新的String
来存储反向单词。最后,添加从Stack
到 的每个字符String
。这将首先拉下最后一个字符,然后是倒数第二个,依此类推。
Note: I believe you have to use the Character
wrapper class, rather than the primitive char
; I may be incorrect about that though.
注意:我相信你必须使用Character
包装类,而不是原始的char
;不过我可能是不正确的。
If you aren't familiar with how Stack
s work, here is a nice interactive tool to visualize it: http://www.cise.ufl.edu/~sahni/dsaaj/JavaVersions/Stacks/AbstractStack/AbstractStack.htm
如果您不熟悉Stack
s 的工作方式,这里有一个很好的交互式工具来可视化它:http: //www.cise.ufl.edu/~sahni/dsaaj/JavaVersions/Stacks/AbstractStack/AbstractStack.htm
回答by wulfgarpro
Change:
改变:
Stack<String> stack = new Stack<String>();
to be
成为
Stack<Character> stack = new Stack<Character>();
and refactor your methods code as necessary; i.e.
并根据需要重构您的方法代码;IE
What is the easiest/best/most correct way to iterate through the characters of a string in Java?
回答by Elliott Frisch
I did it with a different kind of stack, but I suspect this might help
我用不同类型的堆栈做了它,但我怀疑这可能会有所帮助
private static String reverseWord(String in) {
if (in.length() < 2) {
return in;
}
return reverseWord(in.substring(1)) + in.substring(0, 1);
}
private static String reverseSentence(String in) {
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(in);
while (st.hasMoreTokens()) {
if (sb.length() > 0)
sb.append(' ');
sb.append(reverseWord(st.nextToken()));
}
return sb.toString();
}
public static void main(String[] args) {
String sentence = "Hi dog cat";
String expectedOutput = "iH god tac";
System.out.println(expectedOutput
.equals(reverseSentence(sentence)));
}
Outputs
输出
true