Sequence seq = [a, B ,..., Z, AA, AB ,..., AZ, Ba, BB ,..., BZ ,..., Za, ZB ,..., ZZ, AAA,…] Columns similar to excel are listed in ascending order. Returns the number (0-based) of the sequence seq to any string S = [A-Z] + (any length string consisting of a-Z string.
Solution 1: Calculate the 1-based subscript first, and reduce the return value by 1. Note that this method is not considered to be out of bounds.
Unsigned int columntoindex (const char * const pcol) {If (null = pcol) return 0; unsigned int onebasedssum = 0; const char * P = pcol; while ('\ 0 '! = * P) {onebasedsum = (onebasedsum * 26) + (* P-'A' + 1); P ++;} return onebasedsum-1 ;}
Solution 2: directly calculate the 0-based subscript.
Unsigned int getcolumnidx (const char * const PCH, int CCH) {unsigned int sum = 0; char ch; int I = 0; while (I <CCH & (CH = PCH [I]-'A') <26) {sum = (sum + 1) * 26 + CH; // The key point is here, + 1i ++ before sum;} return sum ;}