April 13, 2011

Full Question:
Write a function (with helper functions if needed) called to Excel that takes an excel column value (A,B,C,D…AA,AB,AC,… AAA..) and returns a corresponding integer value (A=1,B=2,… AA=26..).

The result should be like
   A → 1,
   Z → 26,
   AA → 27, (Problem line indicates  'AA=26', but it's somehow strange. (i.e. Z=26)
   ZBC → 17631.

When considering only capitals, following Java function converts alphabet to decimal.  Excepting input.length, you can use in C program directly.

/* alphabet to decimal */
public int alphaToDecimal(char input[]){
  int ret = 0;  
  int a, b;  
  int i, j;  
  for (i=0; i<input.length; i++) {  
    a = input[i] - 'A' + 1;  
    b = 1; // you can use Math.pow();  
    for(j=0; j<input.length-i-1;j++)  
      b *= 'Z'-'A' + 1;  
    ret += a*b;  
  }  
  return ret; 
}