在句子java中反向单词字母

时间:2014-03-25 16:06:42

标签: java string reverse

对于我的程序,我想在一行文本中反转字母。但是我不想颠倒句子中单词的顺序。 例如,当我输入:“这是一个字符串”

我得到:gnirts a si siht

但我想要:siht si a gnirts

public static String reverseWordCharacters(String text1) {
        String reverse = "";
        int length = text1.length();
        for (int i = length - 1; i >= 0; i--) {
            reverse = reverse + text1.charAt(i);
            System.out.println();
        }
        return reverse;
    }

}

7 个答案:

答案 0 :(得分:3)

String sentence = "This is a string";
String[] words = sentence.split(" ");
String invertedSentece = "";
for (String word : words){
    String invertedWord = "";
    for (int i = word.length() - 1; i >= 0; i--)
        invertedWord += word.charAt(i);
    invertedSentece += invertedWord;
    invertedSentece += " ";
}
invertedSentece.trim();

答案 1 :(得分:1)

答案 2 :(得分:1)

这可能是最简单的方法

import java.util.*;
public class ReverseString{
public static void main(String[] args){
    Scanner sc = new Scanner(System.in);
    System.out.print("Enter any Sentence: ");
    String sentence = sc.nextLine();
    String[] stringArray = sentence.split(" ");
    for(int i = 0; i<stringArray.length; i++){
        String temp = new StringBuilder(stringArray[i]).reverse().toString();
        System.out.print(temp+" ");
    }
    }
}

答案 3 :(得分:0)

private static String revWrord(String s) {
       LinkedList word = new LinkedList();
       Scanner scanfs = new Scanner(s);

        while(scanfs.hasNext()) {
            word.add(scanfs.next());
        }

        StringBuilder output = new StringBuilder();
        StringBuilder temp = new StringBuilder();

        while(true) {
            temp.append(word.poll()).reverse();
            output.append(temp);
            temp.delete(0,temp.length() );

            if(word.isEmpty()) {
                break;
            } else {
                output.append(" ");
            }
        }
        return output.toString();
}

答案 4 :(得分:0)

public static void main(String[] args) {
String s= "This is a string";
System.out.println(s);
String finalrevresestring = "";
String wordreverse = "";
String[] a = s.split(" ");
for (int i = 0; i<=a.length-1; i++) {
    for (int x=a[i].length()-1; x>=0; x--) {
        wordreverse += a[i].charAt(x);
    }
    finalrevresestring += wordreverse;
    finalrevresestring += " ";
    wordreverse = "";
    }
System.out.println("This is reverse: "+finalrevresestring);
}

答案 5 :(得分:-1)

{
        String sentence="This is String";
        String[] words=sentence.split(" ");
        String inverted=" ";
        for(String eachWord : words){           
            for(int i=0;i<eachWord.length();i++){
                inverted+=eachWord.charAt(eachWord.length()-1-i);
            }
            inverted+=" ";
}
        System.out.println("Inverted words in the given sentence is :"+inverted);
    }

答案 6 :(得分:-1)

StringBuffer s=new StringBuffer("I live in India");
int i=0,j=0,w=0;
//w is first position of word
//j is last position of word

while(i<s.lenght){

if(s.charAt[i]==" "||i==s.lenght()-1)
{
j=i;
if(i==s.length-1)
j=i;
else
j--;

swapMethod(w,j);
w=i+1;
}
i++;
}