0

I am having troubles in JAVA. I am trying to get the string to print out backwards for each word.

Example: Hello World would be "olleh dlrow"

Below is my class. thanks for the help.

public class ReadReverse {

    String message = "Hello nice to meet you in CS102";
    String[] myStrings = message.split(" ");  //Makes an array of the words split up by spaces
    String result = "";

    {
        // gets the length of the array and splits it up by each word
        for (int i = 0; i < myStrings[i].length(); i++) {
            message.push(myStrings[i].at(i));
            // sets th length to the length of the word
            int length = message.length();
        //          pops the letter out into the new stack 
            for (i = 0; i < length; i++) {
                result += myStrings;
            }
            // checks to see if it is at the end of the string. 
            if (i != message.length() - 1) {
                result += " ";
            }

        }
    }
}
phflack
  • 2,729
  • 1
  • 9
  • 21
Newb
  • 1
  • 2
    `java.lang.String` doesn't have a `push` method . – Arnaud Feb 14 '18 at 14:24
  • 5
    Possible duplicate of [Reverse a string in Java](https://stackoverflow.com/questions/7569335/reverse-a-string-in-java) – Stultuske Feb 14 '18 at 14:25
  • @Stultuske That's not actually a duplicate - this is asking for each individual word to be reversed, not the entire string. (This question however does fit the bill for a duplicate: https://stackoverflow.com/questions/2441501/reverse-each-individual-word-of-hello-world-string-with-java.) – Michael Berry Feb 14 '18 at 15:30
  • @berry120 a substring of a string is also ... a string. sure, we can point out to combine it with split(" "), but should that be necessary? – Stultuske Feb 15 '18 at 06:54

2 Answers2

2

You can achieve string reversal in many ways.

Have a look into this :

 class ReverseString
    {
        public static void main(String[] args)
        {
            String input = "Hello world";
            String[] words = input.split(" ");
            String reversedString = "";
         for (int i = 0; i < words.length; i++)
        {
           String word = words[i]; 
           String reverseWord = "";
           for (int j = word.length()-1; j >= 0; j--) 
       {
            reverseWord = reverseWord + word.charAt(j);
       }
       reversedString = reversedString + reverseWord + " ";
    }
    System.out.println(input);
    System.out.println(reversedString);

    }

}
1

I would revert word by word in this way:

String input = "Hello world";
String[] words = input.split(" ");
StringBuilder reversed = new StringBuilder(        
for (String word : words) {
     reversed.append(new StringBuilder(word).reverse())
             .append(" ");
}
System.out.println(reversed);

Or using java 8 streams this way :

String input = "Hello world";
StringBuilder reversed = new StringBuilder();
Stream.of(input.split(" "))
      .map(StringBuilder::new)
      .map(StringBuilder::reverse)
      .forEach(word -> reversed.append(word).append(" "));
System.out.println(reversed);

Outputs : olleH dlrow

yvoytovych
  • 871
  • 4
  • 12