【发布时间】:2014-02-13 03:42:35
【问题描述】:
所以我试图将句子中的单词颠倒过来,例如:“Hi dog cat”,会变成“iH god tac”。我想知道我是否可以使用我正在做的事情来实现这一目标。我可以让句子本身反转,但我不能让单个单词这样做。有没有办法用字符串来做到这一点,还是我必须弄乱字符(这也很混乱)?任何帮助表示赞赏
private static String PrintStack(String sentence)
{
String reverse = "";
String stringReversed = "";
String Last;
String First;
Stack<String> stack= new Stack<String>();
String words[] = sentence.split(" ");
Last = words[words.length-1];
for(int j = 0; j < words.length; j++)
{
String newWord = words[0+j];
stack.push(newWord);
System.out.println(stack);
}
while(!stack.isEmpty())
{
stringReversed += stack.pop();
}
System.out.println("Reverse is: " + stringReversed);
return reverse;
}
}
【问题讨论】:
-
单字符串或字符。或者实现一个非对象堆栈并使用字符。选择一个。
-
您好,如果您向右看 (===>),相关链接会显示几个类似的问题。如果您花 10 秒时间在 SO 上进行搜索,您会发现类似的问题已经得到解答。请阅读help center 和How to Ask。