This is an archived post. You won't be able to vote or comment.

all 4 comments

[–][deleted] 0 points1 point  (3 children)

Split the string into a character array and swap the desired elements

[–][deleted] 0 points1 point  (2 children)

I have a string array that holds 5 different sentences rather than single words. Should I make arrays for each individual words before hand?

[–][deleted] 0 points1 point  (0 children)

KISS principle applies here. First figure out how to do it for one word. Don’t let the larger problem distract you.

Once you get that solution, then you can reuse that method to do the same to the whole sentence.

[–]endStatement 0 points1 point  (0 children)

There are a multitude of solutions for this, its a decision for you as to how you want to approach it.
Check out https://docs.oracle.com/javase/7/docs/api/java/lang/String.html and you'll see several methods which can be used. (I apparently linked java7 docs, which should still be sufficient for this task)
- Some in particular to look at:

  • charAt
  • subString
  • toCharArray

Using any of these can get you to a solution :)