1.2 Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)
Use BufferBuilder's setCharAt method, switch the char of string head and end one by one.
public class ccChap1_2 {
public static void main(String[] args){
StringBuilder str = new StringBuilder("AK47 M4A1 ");
ccChap1_2.cstringReverse(str);
System.out.println(str);
}
public static StringBuilder cstringReverse(StringBuilder str){
char a=0;
for(int i=0; i<(str.length()/2);i++){
a=str.charAt(i);
str.setCharAt(i, str.charAt(str.length()-i-1));
str.setCharAt(str.length()-i-1,a);
}
return str;
}
}