反转字符串

时间:2012-07-22 07:44:15

标签: java string stringtokenizer

以下程序应该这样做:

当用户输入: The quick brown fox. Jumps over. The lazy dog.
它应输出:God yzal eth. Revo spmuj. Xof nworb kciuq eht.

但是当用户输入时:The quick brown fox. Jumps over. The lazy dog.
它输出:xof nworb kciuq ehT. revo spmuJ . god yzal ehT .

所以这是我的代码:

import java.util.*;
public class Reverse {
public static void main( String args[] ) {
    String paragraph;
    Scanner input = new Scanner (System.in);

    System.out.print("Enter a paragraph: ");
    paragraph = input.nextLine();
    paragraph = paragraph.trim();
    StringTokenizer tokens = new StringTokenizer(paragraph, ".");
    while (tokens.hasMoreTokens()){
        String ss = tokens.nextToken();
        for (int i = ss.length() - 1; i>=0; i--) {
            System.out.print(ss.charAt(i));
        }
        System.out.print(". ");
    }
    System.out.println();
}
}

我也想到了这个:

import java.util.*;

public class Reverse{

    public static void main(String[] args) {
    String paragraph;
    Scanner input = new Scanner (System.in);

    System.out.print("Enter a paragraph: ");
    paragraph = input.nextLine();
    paragraph = paragraph.trim();

    StringTokenizer parWordTokenizer = null;
    StringTokenizer parDotTokenizer = null;
    int numParDotTokens = 0;
    int numParWordTokens = 0;

    parDotTokenizer = new StringTokenizer (paragraph, ".");
    parWordTokenizer = new StringTokenizer (paragraph);
    numParDotTokens = parDotTokenizer.countTokens();
    numParWordTokens = parWordTokenizer.countTokens();

    String[] sentences = new String[numParDotTokens];
    String[] words = new String[numParWordTokens];
    String[] characters = new String[5];

    for (int i=0; i<numParDotTokens; i++) {
        sentences[i] = parDotTokenizer.nextToken();
        System.out.print("S: "+sentences[i]+"\n");
        parWordTokenizer = new StringTokenizer (sentences[i]);
        numParWordTokens = parWordTokenizer.countTokens();
        while (parDotTokenizer.hasMoreTokens()){
            String ss = parDotTokenizer.nextToken();
            for (int k = ss.length() - 1; k>=0; k--) {
                System.out.print(ss.charAt(k));
            }
            System.out.print(". ");
        }
        for (int j=0; j<numParWordTokens; j++) {
            words[j] = parWordTokenizer.nextToken();
            System.out.print("W: "+words[j]+"\n");
            while (parWordTokenizer.hasMoreTokens()){
                String ss = parWordTokenizer.nextToken();
                for (int k = ss.length() - 1; k>=0; k--) {
                    System.out.print(ss.charAt(k));
                }
                System.out.print(". ");
            }
        }
        }
    } } 

revo spmuJ . god yzal ehT . kciuq. nworb. xof. Exception in thread "main" java.util.NoSuchElementException at java.util.StringTokenizer.nextToken(Unknown Source) at ScrambleParagraph.main(Reverse.java:40) 请帮我解决。我一直在考虑正确的实现几天,但我是Java的新手,所以我不能真正做到这一点,至少现在。非常感谢你!

3 个答案:

答案 0 :(得分:1)

这大致是我会这样做的:

public static void main(String[] args) {

    String paragraph;
    Scanner input = new Scanner(System.in);

    System.out.print("Enter a paragraph: ");
    paragraph = input.nextLine();
    paragraph = paragraph.trim();

    String reversedParagraph = new StringBuilder(paragraph)
            .reverse().toString().toLowerCase();

    StringBuilder out = new StringBuilder();
    for (String sentence : reversedParagraph.split(" *\\.")) {
        if (!sentence.equals("")) {
            sentence += ". ";
            out.append(Character.toUpperCase(sentence.charAt(0))
                    + sentence.substring(1));
        }
    }
    System.out.println(out.toString().trim());

}

根据输入:"The quick brown fox. Jumps over. The lazy dog."

这会产生:"God yzal eht. Revo spmuj. Xof nworb kciuq eht."

这似乎是你要求的。

特殊成分是:StringBuilder.reverse()可以反转一个字符串,String.split(regex)可以根据正则表达式将字符串拆分成多个部分。在我颠倒之后,我用这个循环遍历段落中的每个句子。

答案 1 :(得分:1)

这样会更加简单。

public String inverse(String str) {
    StringBuffer strBuf= new StringBuffer(str.trim());
    return strBuf.reverse().toString();         
}

答案 2 :(得分:-2)

如何用“。”分割输入。首先然后反转每个元素?

public class Reverse {

    public static void main(String[] args) {
        String input = "The quick brown fox. Jumps over. The lazy dog.";
        String[] sentences = input.split("\\.");
        StringBuilder result = new StringBuilder();
        for (int i = sentences.length - 1; i >= 0; i--) {
            String sentence = sentences[i];
            result.append(".").append(
                    new StringBuilder(sentence).reverse().toString());
        }
        System.out.println(result);
    }
}

输出:

.god yzal ehT .revo spmuJ .xof nworb kciuq ehT