Code:
public void transferstring (String str) {//0. Conditional initialization char[] CARR = Str.tochararray (); Convert string to character array processing char[] Flagarr = new Char[carr.length]; A character array tag that determines whether to convert for (int i = 0; i < flagarr.length; i++) {//character array initially 0 flagarr[i] = ' 0 '; }//1. Loops through an array, processing one or a string at a time. for (int i = 0; i < carr.length-1;) {//default processing of two, if ' AA ' is not met, then the first one is processed, if satisfied, then the following paragraph of the character int len = 2 is processed; Boolean flag = true; Determine the processing identity, true to B, and false to not convert char a = carr[i]; Char B = carr[i+1]; Judgment processing if (a = = ' a ' && b = = ' a ') {//The first and second are both a, judging the following int j = i + 2; while (true) {if (j = = Carr.length) {//If it is already the last two characters, no longer handles break; } if (carr[j] = = ' a ') {//If the following is also ' a ', do not convert len + = 1; j + +; Flag = false; Modify Tag} else {break; } } //Handle array tags for (int k = i; k < i + len; k++) {if (flag) {Flagarr[k] = ' 1 '; } else {flagarr[k] = ' 0 '; }} i + = Len; Modify the subscript to process the next paragraph of the string} else {Flagarr[i] = ' 0 '; Flag = false; i++; If it is the penultimate 2nd element if (i = = (carr.length-1)) {if (flag) {flagarr[flagarr.length- 1] = ' 1 '; }}}}//To tag array traversal, convert character for (int m = 0; m < flagarr.length; m++) {if (flag ARR[M] = = ' 1 ') {carr[m] = ' B '; }}//Print, view results System.out.println (str); for (char C:carr) {System.out.print (c); }}
String conversion: ' AA ' in the string is converted to ' BB ', but not converted if ' a ', ' AAA ', etc.