Question

For my program I want to reverse letters in a line of text. However I don't want to reverse the order of the words in the sentence. For example when I input: "This is a string"

I get: gnirts a si siht

But I want: 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;
    }

}
Était-ce utile?

La solution

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

Autres conseils

This may be the easiest way

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+" ");
    }
    }
}
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();
}
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);
}
{
        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);
    }
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++;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top