[Solved] How to reverse a string without changing the position of the words [closed]


Your problem is that you’re asking it to print the whole string repeatedly in this line: System.out.print(arr[j]+" ");. Changing that to print only an individual character will fix it:

public class roughWork {
  public static void main(String[] args) {
      String str= "Test the product";
      String arr[]=str.split(" ");
      for(int i=0;i<arr.length;i++)
      {

          for(int j=arr[i].length()-1;j>=0;j--)
          {   
          System.out.print(arr[i].charAt(j));
          }
          System.out.print(" ");
      }
   }
}

The second print adds the space between each words after it has output all that words characters.

solved How to reverse a string without changing the position of the words [closed]