微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

检测堆栈中的周期

如何解决检测堆栈中的周期

您好,我目前正在开发一个使用堆栈反转句子的程序。但是,我需要实现一个检测周期然后再次重复循环的函数。例如,我可以写“你好,我的名字是空白的。很高兴认识你。”它会打印“you. meet to nice blank. is name my Hello”,当我实际上只需要它来单独反转每个句子时,如果这有意义的话。所以“空白是我的你好。你见面就很好。”是我想要的输出。任何建议都会有很大帮助,谢谢!

import java.util.Stack;
import java.util.Scanner;

public class NolascoStackReverse {

 public static void main (String[] args) {
  
      Scanner in = new Scanner(system.in);
      Stack<String> reverse = new Stack<String>();
      
      System.out.println("Enter a sentence to be reversed: ");
      String str = in.nextLine();

      String[] wordsArray = str.split(" ");
      
      for (String word : wordsArray) {
          reverse.push(word);
      }
      System.out.println("Here is the reversed order: ");
      
      while (reverse.empty() == false) {
            System.out.print(reverse.pop() + " ");
      }

   }
}

解决方法

下面的代码用空格字符分割你的字符串。你需要先用句号分割,然后用空格分割每个句子。

    String[] wordsArray = str.split(" ");

如果你不关心句号,那么你可以使用多个分隔符来分割函数:Use String.split() with multiple delimiters

如果你需要反句中的句号,你可以使用下面的代码:

    String[] sentenceArray = str.split(("\\."));
    for (String sentence : sentenceArray){
        String[] wordsArray = sentence.split((" "));
        for (String word : wordsArray) {
            reverse.push(word);
        }
        reverse.push(".");
    }
,

Java 中已经有一个 API 可用于处理文本。您可以像这样使用 BreakIterator

public class Test {
    public static void main(String[] args) throws Exception {
         Stack<String> reverse = new Stack<String>();
         String result="";
         Locale enUS = new Locale("en","US");
         
         String str = "My name is R.R.Martin. Nice to meet you.";
         System.out.println("Text: " + str);
         System.out.print("Here is the reversed order: ");
         
         List<String> sentences = getPieces(str,BreakIterator.getSentenceInstance(enUS));
         for(String s : sentences) {
             s = s.substring(0,s.length()-1);
             
             List<String> words = getPieces(s,BreakIterator.getWordInstance(enUS));
             reverse.clear();
             for (String w : words) {
                 reverse.push(w.strip());
             }
             while (reverse.empty() == false) {
                   result += reverse.pop() + " ";
             }
             result = result.strip();
             result += result.length()>0? ". ":""; 
         }
         System.out.println(result);
    }
    
    public static List<String> getPieces(String text,BreakIterator bi) {
        int boundary,start;
        String piece = "";
        List<String> list = new ArrayList<>();

        bi.setText(text);
        boundary = bi.first();
        start = 0;
        while (boundary != BreakIterator.DONE) {
            if(boundary>0)
                piece = text.substring(start,boundary);
            start = boundary;
            boundary = bi.next();
            if(!piece.strip().isBlank())
                list.add(piece.strip());
        }
        return list;
    }

}

输出:

Text: My name is R.R.Martin. Nice to meet you.
Here is the reversed order: R.R.Martin is name My. you meet to Nice. 


这种方法的优点是它对区域设置敏感,即您可以将代码用于其他语言。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。