package homework_1;
public class Pancount1 {
/*反转方法*/
public static void swap(char[] c, int i, int j) {
while (i < j) {
char tmp = c[j];
c[j] = c[i];
c[i] = tmp;
i++;
j--;
}
}
public static void main(String[] args) {
//定义字符串
String str = "I am dxw,I like study.Java!";
char[] ch = str.toCharArray();
// 1、反转字符串
swap(ch, 0, ch.length - 1);
// 2、对每个单词以" "或符号为分割点进行字符反转操作
int start = 0;
for (int i = 1; i < ch.length; i++) {
if (ch[i] == ' ' || ((int) ch[i] >= 34 && (int) ch[i] <= 47)) {
swap(ch, start, i - 1);
start = i + 1;
}
}
// 3、将符号转化为' '.
for (int i = 0; i < ch.length; i++) {
if (((int) ch[i] >= 33 && (int) ch[i] <= 47)) {
ch[i] = ' ';
}
}
//4、输出反转后的字符串
for(int i=0;i<ch.length;i++) {
System.out.print(ch[i]);
}
}
}