I am trying to reverse words in a String in Java. It is initialliy passed as a char array. The way I tried is to convert it into a String array, make the change, and convert it back to a char array. However, changes are not made to the original array that is passed in as an argument, even if I tried to refer the new array to the original one. How do I handle this situation when we have to make modifications to the argument without returning anything in the function? Thanks.
public static void main(String[] args) {
char[] s = new char[] {'t','h','e',' ','s','k','y',' ','i','s',' ','b','l','u','e'};
reverseWords(s);
System.out.println(s);
}
public static void reverseWords(char[] s) {
String str = new String(s);
String [] strArray = str.split(" ");
int n = strArray.length;
int begin;
int end;
int mid = (n-1)/2;
for (int i=0; i<=mid; i++) {
begin = i;
end = (n-1) -i;
String temp = strArray[begin];
strArray[begin] = strArray[end];
strArray[end] = temp;
}
String s_temp = Arrays.toString(strArray).replace("[", "").replace("]", "").replace(",", "");
s = s_temp.toCharArray();
System.out.println(s);
}