Sunday, 17 July 2016

Excel Column Number

/*  Excel Column Number */
/*
    Given a column title as appears in an Excel sheet, return its corresponding column number.

    Example:
    A -> 1
    B -> 2
    C -> 3

    ...
    Z -> 26
    AA -> 27
    AB -> 28
*/

 #include<iostream>  
 #include<string.h>  
 #include<cmath>  
 using namespace std;  
 int colNumber(char *s){  
   int i, len = strlen(s);  
   int out = 0, j = 0, digit;  
   for(i = len - 1; i >= 0; i--){  
     digit = s[i] - 'A' + 1;  
     out = out + pow(26, j) * digit;  
     j++;  
   }  
   return out;  
 }  
 int main(){  
   char s[] = "ABB";  
   cout << colNumber(s);  
   return 0;  
 }  
Share:

0 comments:

Post a Comment

Contact Me

Name

Email *

Message *

Popular Posts

Blog Archive