【问题标题】:Splitting a sentence into words using for loop or while loop in java在java中使用for循环或while循环将句子拆分为单词
【发布时间】:2013-05-17 04:24:22
【问题描述】:

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

输出:aidnI si ym yrtnuoc

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

导入 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 siaidnI 我还没学过数组...

【问题讨论】:

  • 发布你的代码sn-p
  • 不会将“印度是我的国家”“逐字逐句”颠倒成“我的国家是印度”吗?
  • 他的意思是在你的问题中发布你的 sn-p。

标签: java reverse


【解决方案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);

【讨论】:

    【解决方案2】:

    这通过了测试:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        【解决方案4】:

        试试这个

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

        【讨论】:

          猜你喜欢
          • 2020-06-20
          • 1970-01-01
          • 2013-07-21
          • 2021-03-27
          • 1970-01-01
          • 2017-11-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多