简体   繁体   English

如何在不使用数组、split() 或 StringBuilder 的情况下逐字反转字符串

[英]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.

相关问题 java:使用StringBuffer而不使用String.split的反词 - java: reverse word using StringBuffer without using String.split 使用StringBuilder和BufferedReader,如何在文本文件中的每个单词后添加逗号并生成字符串? - Using StringBuilder and BufferedReader, how can I add commas after each word in a text file and produce a string? 如何在没有分隔符的情况下拆分单词并对 Java 8 中的拆分字符串执行操作? - How I can split a word without a delimiter and perform operations over the split string in Java 8? 如何在某个单词后使用.split拆分字符串? - How can I use .split to split a string AFTER a certain word? 如何在不使用数组的情况下在java字符串中找到最短的单词? - How to find shortest word in a java string without using arrays? 在java中逐字反转字符串(不使用StringBuilder) - Reversing string by word in java (not using StringBuilder) 如何删除 StringBuilder 中句子的最后一个单词? - How can I delete last word of sentence in StringBuilder? 如何在不使用.split()的情况下计算字符串中的每个单词? - How to count each word in a string without .split()? 如何在不丢失单词的情况下分割字符串? - How to split a string without losing any word? 在字符串中逐字逆序 - Reverse word by word in a String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM