The code below works when number of strings in array is odd (3,5,7) however it does not work when the number is even. For example, if I use "my is the name" I get output
name is the name
public void randomTest() {
String str ="my is name";
//Step1: split so that I can get them in in array
String [] arrStr= str.split(" ");
for(int i=0;i<arrStr.length;i++){
//Step2: Using temp swap 1st and last
String temp = arrStr[0];
arrStr[0] = arrStr[arrStr.length-1];
arrStr[arrStr.length-1] = temp;
System.out.print(arrStr[i]+" ");
}
}
Any idea how can I make it work for even number of Strings? Thank You.