翻译句子中的每个单词

时间:2013-11-03 04:23:22

标签: java stack reverse

我想用向后的每个单词的字符打印句子。

实施例:  狐狸跳过篱笆。

变为:  ehT xof depmuj revo eht ecnef。

我只能反转第一个单词,然后它给了我一个ArrayIndexOutOfBoundsException。

6 个答案:

答案 0 :(得分:4)

尝试以下简单代码。这更容易:

String[] words = sentence.split(" ");
for (String word : words) {
    System.out.print(new StringBuilder(word).reverse() + " ");
}

答案 1 :(得分:3)

您需要对代码进行一些更改:

1.使用sentence.length()

更改words.length

2.使用read.nextLine();将句子作为输入

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

public class test {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in); // Error in line 11
        System.out.print("Enter a sentence: ");
        String sentence = read.nextLine();
        boolean containsPeriod= false;
        if (".".equals(sentence.substring(sentence.length()-1,sentence.length()))){
               sentence = sentence.substring(0,sentence.length()-1);
               containsPeriod=true;
           }


        String[] words = sentence.split(" ");
        for (int i = 0; i < words.length; i++) {
            String word = words[i]; // Error in line 23
            System.out.print(reverseWord(word));
            if(i!=words.length-1)
                System.out.print(" ");
        }
        if(containsPeriod)
            System.out.print(".");
        read.close();
    }

    private static String reverseWord(String sentence) {
        Stack<Character> rev = new Stack();
        for (int ii = 0; ii < sentence.length(); ii++) {
            rev.push(sentence.charAt(ii));
        }
        String out = "";
        while (rev.size() != 0) {
            out += rev.pop();
        }
        return out;
    }
}

答案 2 :(得分:2)

在第一个sentence.length()循环中将words.length替换为for

你想在句子中的每个单词中迭代一次这个循环;但是你写的代码是在句子中每个字母迭代一次。那是你的错误。

答案 3 :(得分:1)

您可以修改代码,如下所示

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;

public class Query1 {
    public static void main(String[] args) {
        Scanner read = new Scanner(System.in); // Error in line 11

        System.out.print("Enter a sentence: ");
        // String sentence = read.next();
            // read full line here instead of just one word
        String sentence = read.nextLine();
        String[] words = sentence.split(" ");
            // this looks goes to array length
        // for (int i = 0; i < sentence.length(); i++) {
        for (int i = 0; i < words.length; i++) {
            String word = words[i]; // Error in line 23
            // System.out.println(reverseWord(word));
                    // use print not println
            System.out.print(reverseWord(word));
                    // this loop is not at all required
            // for (i = 0; i < words.length - 1; i++) {
            // System.out.println(" ");
            // }
            System.out.print(" ");
        }

    }

    private static String reverseWord(String sentence) {
        Deque<Character> rev = new ArrayDeque<Character>();
        // ArrayStack<Character> rev = new ArrayStack<Character>();

        for (int ii = 0; ii < sentence.length(); ii++) {
            rev.push(sentence.charAt(ii));
        }
        String out = "";
        while (rev.size() != 0) {
            out += rev.pop();

        }

        return out;
    }
}

答案 4 :(得分:0)

试试这个

public static void main(String args[]) {
        String input = "The fox jumped over the fence";
        StringBuilder sbReverseSentance = new StringBuilder();
        String words[] = input.split(" ");
        for (String word : words) {
            for (int i = word.length() - 1; i >= 0; i--) {
                sbReverseSentance.append(word.charAt(i));
            }
              sbReverseSentance.append(" ");
        }
        System.out.println (sbReverseSentance.toString());
    }

答案 5 :(得分:0)

试试这个,它不处理标点符号。

import java.util.Scanner;

public class Test {

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

    System.out.print("Enter a sentence: ");
    String sentence = read.nextLine();

    String[] words = sentence.split(" ");

    String[] reversed_words = new String[words.length];

    for (int i = 0; i < words.length; i++) {
        reversed_words[i] = reverseWord(words[i]);
    }

    for (int i = 0; i < reversed_words.length; i++)
        System.out.println(reversed_words[i]);

}

private static String reverseWord(String word) {
    String result = "";
    char[] array = word.toCharArray();
    for (int i = array.length - 1; i >= 0; i--) {
        result += array[i];
    }
    return result;
}
}