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

问题描述:

我必须做一个程序,它需要一个句子并在java中逐字地反转它。对于如: 印度是我的国家使用for循环或while循环将句子拆分为单词java中的循环或while循环

输出:aidnI SI YM yrtnuoc

香港专业教育学院想通了这一切,但我只是不能拆分句子转化成单独的words.im不允许使用分割功能,但即时通讯意思使用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 我还没有了解到阵列尚未...

+0

发表您的代码段 – prasanth

+0

不会逆转“印度是我的祖国”“一字一句”让它出来'我的国家是印度? – Craig

+0

他的意思是在你的问题中发布你的代码片段。 – greedybuddha

我打算假设先进的数据结构已经出来,效率不是问题。

如果你要错误的是你正在反转整个字符串,你只需要反转这些单词。所以你真的需要检查以找出单词结束的地方,然后或者反过来,或者在你前进的时候反转它。

下面是一个逆转的例子。

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

非常感谢你:) – aqua

这通过测试:

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


} 

这是我的代码没有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()); 
    } 

} 

试试这个

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