在java中使用for循环或while循环将句子拆分为单词

时间:2013-05-17 04:24:23

标签: java reverse

我必须做一个程序,它接受一个句子并在java中逐字翻转。例如: 印度是我的国家

输出:aidnI si ym yrtnuoc

我想出了所有这些,但我只是不能将一个句子分成单独的单词。我不允许使用split函数,但我的意思是使用substring或indexof(); while循环和for循环是允许的。 这是我到目前为止所得到的:

import java.io。*;

公共课Rereprogram10

{

public void d()throws IOException

{

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

 String str;

 System.out.println("input a string");


 str=br.readLine();

 String rev="";
 int length=str.length();
 int counter=length;
 for(int i=0;i<length;i++)
 {
     rev=rev+str.charAt(counter-1);
     counter--;
    }
    System.out.println("the result is: "+rev);
}

}

它的错误,输出继续: yrtnuoc ym si aidnI 我还没学过阵列......

4 个答案:

答案 0 :(得分:1)

我将假设高级数据结构已经淘汰,效率不是问题。

你出错的地方是你正在翻转整个字符串,你只需要翻转单词。所以你真的需要检查以找到一个单词结束的位置,然后将其反转,或者在你继续时将其反转。

以下是一个逆转的例子。

int length=str.length();
String sentence="";
String word = "";
for(int i=0;i<length;i++) {
    if (str.charAt(i) != ' '){
        word = str.charAt(i) + word;
    } else {
        sentence += word +" ";
        word = "";
    }
}
sentence += word;
System.out.println("the result is: "+sentence);

答案 1 :(得分:0)

这通过了测试:

package com.sandbox;

import com.google.common.base.Joiner;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static org.junit.Assert.assertEquals;

public class SandboxTest {

    @Test
    public void testQuestionInput() {
        String input = "India is my country";

        assertEquals("country my is India", reverseWords(input));
    }

    private String reverseWords(String input) {
        List<String> words = putWordsInList(input);

        Collections.reverse(words);

        return Joiner.on(" ").join(words);
    }

    private List<String> putWordsInList(String input) {
        List<String> words = new ArrayList<String>();
        String word = "";
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (c == ' ') {
                words.add(word);
                word = "";
            } else {
                word += c;
            }
        }
        words.add(word);
        return words;
    }


}

答案 2 :(得分:0)

这是我没有split()的代码。

输入: 印度是我的国家

输出:

国家我是印度

aidnI si ym yrtnuoc

您可以选择所需的输出。

public class Reverse {
    public static class Stack {
    private Node[] slot = new Node[1000];
    private int pos = 0;
    private class Node{
        private char[] n = new char[30];
        private int pos = 0;
        public void push(char c) {
            n[pos++] = c;
        }
        public String toString() {
            return new String(n).trim() + " "; // TODO Fix
        }
    }

    public void push(char c) {
        if(slot[pos] == null)
            slot[pos] = new Node();

        if(c != ' ') {
            slot[pos].push(c);
        } else {
            slot[pos++].push(c);
        }
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        for(int i = pos; i >=0; i --)
            sb.append(slot[i]);
        return sb.toString();
    }

    private String reverseWord(String word) {
        StringBuilder sb = new StringBuilder();
        int len = word.length();
        for(int i = len - 1; i >= 0; i--)
            sb.append(word.charAt(i));
        return sb.toString();
    }
    public String foryou() {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < pos + 1; i ++)
            sb.append(this.reverseWord(slot[i].toString()));
        return sb.toString();
    }
}
/**
 * @param args
 */
public static void main(String[] args) {
    Stack stack = new Stack();
    String sentence = "India is my country";
    System.out.println(sentence);
    for(int i = 0; i < sentence.length(); i ++) {
        stack.push(sentence.charAt(i));
    }
    System.out.println(stack);
    System.out.println(stack.foryou());
    }

}

答案 3 :(得分:0)

试试这个

String x="India is my country";
StringBuilder b=new StringBuilder();


int i=0;
do{
     i=x.indexOf(" ", 0);
     String z;
     if(i>0){
         z=x.substring(0,i); 
     }
     else{
         z=x;
     }

     x=x.substring(i+1);
     StringBuilder v=new StringBuilder(z);
     b.append(v.reverse());
     if(i!=-1)
     b.append(" ");
     System.out.println(b.toString());

}
while(i!=-1);
相关问题