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

在java中使用for循环或while循环将句子拆分为单词,java,reverse,Java,Reverse,我必须做一个程序,它接受一个句子,并在java中逐字颠倒它。例如: 印度是我的国家 输出:aidnI si ym yrtnuoc 我把它都弄明白了,但我就是不能把一个句子分成几个词。我不允许使用split函数,但我想使用substring或indexof();而允许循环和for循环。 这就是我目前所得到的: 导入java.io.* 公共类重新编程10 { public void d()引发IOException { BufferedReader br=新的BufferedReader(新的Inp

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

输出:aidnI si ym yrtnuoc

我把它都弄明白了,但我就是不能把一个句子分成几个词。我不允许使用split函数,但我想使用substring或indexof();而允许循环和for循环。 这就是我目前所得到的:

导入java.io.*

公共类重新编程10

{

public void d()引发IOException
{
BufferedReader br=新的BufferedReader(新的InputStreamReader(System.in));
字符串str;
System.out.println(“输入字符串”);
str=br.readLine();
字符串rev=“”;
int length=str.length();
int计数器=长度;
对于(int i=0;i这通过了测试:

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;
    }


}
package.com.sandbox;
导入com.google.common.base.Joiner;
导入org.junit.Test;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入静态org.junit.Assert.assertEquals;
公共类沙盒测试{
@试验
公共void testQuestionInput(){
字符串输入=“印度是我的国家”;
assertEquals(“我的国家是印度”,reverseWords(输入));
}
私有字符串反向记录(字符串输入){
列表字=输入字列表(输入);
收藏。反面(文字);
返回Joiner.on(“”).join(字);
}
私有列表putWordsInList(字符串输入){
List words=new ArrayList();
字串=”;
对于(int i=0;i
我假设高级数据结构已经过时,效率不是问题

错误的地方在于你正在反转整个字符串,你只需要反转单词。所以你真的需要检查单词的结尾,然后要么反转它,要么在你继续的过程中反转它

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);
这是一个随车倒车的例子

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);
int-length=str.length();
字符串句子=”;
字串=”;

对于(int i=0;i这是我的代码,不带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());
    }

}
公共类反向{
公共静态类堆栈{
专用节点[]插槽=新节点[1000];
私人int pos=0;
私有类节点{
私有字符[]n=新字符[30];
私人int pos=0;
公共无效推送(字符c){
n[pos++]=c;
}
公共字符串toString(){
返回新字符串(n).trim()+“”;//TODO Fix
}
}
公共无效推送(字符c){
如果(插槽[pos]==null)
slot[pos]=新节点();
如果(c!=''){
槽[pos]。推送(c);
}否则{
插槽[pos++]。推送(c);
}
}
公共字符串toString(){
StringBuilder sb=新的StringBuilder();
对于(int i=pos;i>=0;i--)
某人追加(槽[i]);
使某人返回字符串();
}
专用字符串反转符(字符串字){
StringBuilder sb=新的StringBuilder();
int len=word.length();
对于(int i=len-1;i>=0;i--)
某人附加(单词charAt(i));
使某人返回字符串();
}
公共字符串foryou(){
StringBuilder sb=新的StringBuilder();
对于(int i=0;i
试试这个

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);

把你的代码片段贴出来“印度是我的国家”不是逐字逐句地写出来“我的国家是印度”吗?他的意思是在你的问题中贴出你的代码片段。