1

我试图制作一个程序来从扫描仪中获取字符串的输入,但我想分解输入的字符串并颠倒单词的顺序。这就是我到目前为止所拥有的。

Scanner input = new Scanner(System.in);
System.out.println("Enter your string");
StringBuilder welcome = new StringBuilder(input.next());
int i;
for( i = 0; i < welcome.length(); i++ ){
    // Will recognize a space in words
    if(Character.isWhitespace(welcome.charAt(i))) {
        Character a = welcome.charAt(i);
    }   
}

我想要做的是在它识别空间之后,捕获它之前的所有内容,等等每个空间,然后重新排列字符串。

4

7 回答 7

4

问题后编辑。

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

public class Main {

   public static void main( String[] args ) {
      final String welcome = "How should we get words in string form a List?";
      final List< String > words = Arrays.asList( welcome.split( "\\s" ));
      Collections.reverse( words );
      final String rev = words.stream().collect( Collectors.joining( ", " ));
      System.out.println( "Your sentence, reversed: " + rev );
   }
}

执行:

Your sentence, reversed: List?, a, form, string, in, words, get, we, should, How
于 2013-04-09T19:19:26.020 回答
3

我确实建议先反转整个字符串。然后反转两个空格之间的子字符串。

public class ReverseByWord {

    public static String reversePart (String in){
        // Reverses the complete string
        String reversed = "";
        for (int i=0; i<in.length(); i++){
            reversed=in.charAt(i)+reversed;
        }
        return reversed;
    }

    public static String reverseByWord (String in){
        // First reverses the complete string
        // "I am going there" becomes "ereht gniog ma I"
        // After that we just need to reverse each word.
        String reversed = reversePart(in);
        String word_reversal="";
        int last_space=-1;
        int j=0;
        while (j<in.length()){
            if (reversed.charAt(j)==' '){
                word_reversal=word_reversal+reversePart(reversed.substring(last_space+1, j));
                word_reversal=word_reversal+" ";
                last_space=j;
            }
            j++;
        }
        word_reversal=word_reversal+reversePart(reversed.substring(last_space+1, in.length()));
        return word_reversal;
    }

    public static void main(String[] args) {
        // TODO code application logic here
        System.out.println(reverseByWord("I am going there"));
    }
}
于 2013-04-09T19:17:59.857 回答
1

这是您可以在输入的字符串中反转单词的方法:

Scanner input = new Scanner(System.in);
System.out.println("Enter your string");
String s = input.next();

if(!s.trim().contains(' ')) {
   return s;
}
else {

   StringBuilder reversedString = new StringBuilder();
   String[] sa = s.trim().split(' ');

   for(int i = sa.length() - 1; i >= 0: i - 1 ) {

      reversedString.append(sa[i]);
      reversedString.append(' ');
   }

   return reversedString.toString().trim();
}

希望这可以帮助。

于 2013-04-09T19:28:08.350 回答
1

如果您想减少代码行数,我想您可以查看我的代码:

package com.sujit;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class StatementReverse {
    public static void main(String[] args) throws IOException {
        String str;
        String arr[];
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a string:");
        str = br.readLine();
        arr = str.split("\\s+");

        for (int i = arr.length - 1;; i--) {
            if (i >= 0) {
                System.out.print(arr[i] + " ");
            } else {
                break;
            }

        }
    }
}
于 2017-08-18T09:45:10.400 回答
0
public class StringReverse {
    public static void main(String[] args) {
        String str="This is anil thakur";
        String[] arr=str.split(" ");

        StringBuilder builder=new StringBuilder("");
        for(int i=arr.length-1; i>=0;i--){
            builder.append(arr[i]+" ");
        }
        System.out.println(builder.toString());
    }

}

Output: thakur anil is This 
于 2015-12-02T10:14:33.920 回答
0
public class ReverseWordTest {

  public static String charRev(String str) {

    String revString = "";
    String[] wordSplit = str.split(" ");

    for (int i = 0; i < wordSplit.length; i++) {

      String revWord = "";
      String s2 = wordSplit[i];

      for (int j = s2.length() - 1; j >= 0; j--) {
        revWord = revWord + s2.charAt(j);
      }

      revString = revString + revWord + " ";
    }
    return revString;
  }

  public static void main(String[] args) {
    System.out.println("Enter Your String: ");
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    System.out.println(charRev(str));
  }
于 2018-04-10T07:08:22.860 回答
0
    public static void main(String[]args)
{
String one="Hello my friend, another way here";
String[]x=one.split(" ");
one="";
int count=0;
for(String s:x){
    if(count==0||count==x.length) //that's for two edges.
      one=s+one;
    else
      one=s+" "+one;
    count++;
}
System.out.println(one); //reverse.
}
于 2019-08-03T21:29:42.597 回答