String conversion: ' AA ' in the string is converted to ' BB ', but not converted if ' a ', ' AAA ', etc.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.