Barclays Capital Interview Question
Software Engineer / Developerschar temp =' '; is not an object.
public class Test{
public static void main(String[] args){
char[] ar = {'A','R','G','E','N','T','I','N','A'};
int length = ar.length;
char temp =' ';
for(int i=0; i < length/2; i++){
temp = ar[i];
ar[i] = ar[length -1 -i];
ar[length -1 -i] = temp;
}
for(int i=0; i < length; i++){
System.out.print(ar[i]);
}
}
}
but yes, bitwise exclusive or satisfies even more strict requirements.
public class Reverse {
public static void reverse(char[] arr) {
for (int i = 0; i < arr.length; i++) {
swap(arr, i, arr.length - i);
}
private static void swap(char[] arr, int i, int j) {
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
- Anonymous February 01, 2010