英文:
How can I reverse a string word by word without using arrays, split() or StringBuilder
问题
以下是您要翻译的内容:
我试图逐词反转字符串,但不使用数组,split()
或 StringBuilder
。到目前为止,这是我的代码。它可以工作,但输出不符合我的预期。请查看我附带的图片以获取输出结果。
我希望程序将每个单词输出到新行。同时请注意单词 "curtain" 中缺少字母 "n",并且最后两个单词之间没有空格。我该如何修复这个问题?
public static void main(String[] args) {
String sentence = new String("pay no attention to that man behind the curtain");
String reversed = "";
int endIndex = sentence.length() - 1;
for (int i = endIndex; i >= 0; i--) {
if (sentence.charAt(i) == ' ') {
reversed += sentence.substring(i, endIndex);
endIndex = i;
}
}
reversed += sentence.substring(0, endIndex);
System.out.println(reversed);
}
![在此输入图片描述][1]
[1]: https://i.stack.imgur.com/mpW2R.png
<details>
<summary>英文:</summary>
I'm trying to reverse a string word by word without using arrays, `split()` or `StringBuilder`. This is my code so far. It works, but my output is not how I want it. See my image for output.
I want my program to output each word on a new line. Also notice how the letter "n" is missing from curtain and there is no space between the last two words. How can I fix this?
public static void main(String[] args) {
String sentence = new String("pay no attention to that man behind the curtain");
String reversed = "";
int endIndex = sentence.length()-1;
for(int i = endIndex; i >= 0; i--) {
if(sentence.charAt(i) == ' ') {
reversed += sentence.substring(i, endIndex);
endIndex = i;
}
}
reversed += sentence.substring(0, endIndex);
System.out.println(reversed);
}
![enter image description here][1]
[1]: https://i.stack.imgur.com/mpW2R.png
</details>
# 答案1
**得分**: 1
```java
尝试一下这段代码:
import java.util.Scanner;
public class ReverseString
{
public static void main(String[] args)
{
System.out.println("输入要反转的字符串:");
Scanner read = new Scanner(System.in);
String str = read.nextLine();
String reverse = "";
for(int i = str.length() - 1; i >= 0; i--)
{
reverse = reverse + str.charAt(i);
}
System.out.println("反转后的字符串是:");
System.out.println(reverse);
}
}
英文:
Try this code out:
import java.util.Scanner;
public class ReverseString
{
public static void main(String[] args)
{
System.out.println("Enter string to reverse:");
Scanner read = new Scanner(System.in);
String str = read.nextLine();
String reverse = "";
for(int i = str.length() - 1; i >= 0; i--)
{
reverse = reverse + str.charAt(i);
}
System.out.println("Reversed string is:");
System.out.println(reverse);
}
}
答案2
得分: 1
首先,有一种更好的方法来反转单词。但让我们看看你的程序。
我希望我的程序在每一行输出每个单词。
如果你想要在新的一行中打印每个单词,你可以将每个单词添加到一个单词列表中,并在新的一行中打印每个单词,或者你可以在每个单词的末尾添加 "\n"。
还要注意,“curtain” 中缺少字母 “n”,并且最后两个单词之间没有空格。
这是因为 endIndex
从 sentence.length()-1
开始,而在 Java 中,substring
的工作方式是从 startIndex 到 endIndex - 1,即 endIndex 是独占的,startIndex 是包含的。你可以通过声明 endIndex = sentence.length()
并从 i = sentence.length()-1
迭代到 0
来修复它。
修复后的代码如下:
public static void main(String[] args) {
String sentence = new String("pay no attention to that man behind the curtain");
String reversed = "";
int endIndex = sentence.length();
for(int i = sentence.length()-1; i >= 0; i--) {
if(sentence.charAt(i) == ' ') {
reversed += sentence.substring(i+1, endIndex) + "\n";
endIndex = i;
}
}
reversed += sentence.substring(0, endIndex);
System.out.println(reversed);
}
更好的方法是:
a) 将你的字符串转换为字符数组
b) 然后反转整个字符数组,变成:
niatruc eht dniheb nam taht ot noitnetta on yap
c) 然后在每个空格之间就地反转字母。
d) 这样你将得到表示以下内容的新字符数组:
curtain the behind man that to attention no pay
然后你可以从新的字符数组构造一个字符串。
英文:
First of all, there is a better way to reverse the words. But lets look at your program.
> I want my program to output each word on a new line.
If you want to print each word in a new line, you could either add each word to a list of words and print each word in a new line or you could just add "\n" at the end of each word.
> Also notice how the letter "n" is missing from curtain and there is no
> space between the last two words.
This is because the endIndex
starts at sentence.length()-1
and substring
in Java works by extracting from startIndex to endIndex - 1 i.e. endIndex is exclusive and startIndex is inclusive.
You can fix it by declaring endIndex = sentence.length()
and iterate from i = sentence.length()-1
to 0
.
With that the code would be:
public static void main(String[] args) {
String sentence = new String("pay no attention to that man behind the curtain");
String reversed = "";
int endIndex = sentence.length();
for(int i = sentence.length()-1; i >= 0; i--) {
if(sentence.charAt(i) == ' ') {
reversed += sentence.substring(i+1, endIndex) + "\n";
endIndex = i;
}
}
reversed += sentence.substring(0, endIndex);
System.out.println(reversed);
}
The better way is :
a) Convert your string to character array
b) Then reverse the whole character array which would become :
niatruc eht dniheb nam taht ot noitnetta on yap
c) Then reverse the letters between each space in-place.
d) You will get back the new character array that represents:
curtain the behind man that to attention no pay
and you can construct a string from the new character array.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论