[英]How can I reverse a string word by word without using arrays, split() or StringBuilder
I'm trying to reverse a string word by word without using arrays, split()
or StringBuilder
.我试图在不使用数组、
split()
或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.
还要注意窗帘中的字母“n”是如何丢失的,并且最后两个单词之间没有空格。 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);
}
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);
}
}
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.如果您想在新行中打印每个单词,您可以将每个单词添加到单词列表中并在新行中打印每个单词,或者您可以在每个单词的末尾添加“\\n”。
Also notice how the letter "n" is missing from curtain and there is no space between the last two words.
还要注意窗帘中的字母“n”是如何丢失的,并且最后两个单词之间没有空格。
This is because the endIndex
starts at sentence.length()-1
and substring
in Java works by extracting from startIndex to endIndex - 1 ie endIndex is exclusive and startIndex is inclusive.这是因为
endIndex
开始于sentence.length()-1
并且 Java 中的substring
通过从 startIndex 提取到 endIndex - 1 来工作,即 endIndex 是独占的,而 startIndex 是包括在内的。 You can fix it by declaring endIndex = sentence.length()
and iterate from i = sentence.length()-1
to 0
.您可以通过声明
endIndex = sentence.length()
并从 i = sentence.length()-1
迭代到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 a) 将您的字符串转换为字符数组
b) Then reverse the whole character array which would become : b) 然后反转整个字符数组,它将变成:
niatruc eht dniheb nam taht ot noitnetta on yap
c) Then reverse the letters between each space in-place. c) 然后原地反转每个空格之间的字母。
d) You will get back the new character array that represents: d) 您将取回表示以下内容的新字符数组:
curtain the behind man that to attention no pay
and you can construct a string from the new character array.并且您可以从新的字符数组构造一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.