Programming Riddle: How might you translate an Excel column name to a number?

前端 未结 28 1405
[愿得一人]
[愿得一人] 2020-11-29 22:26

I was recently asked in a job interview to resolve a programming puzzle that I thought it would be interesting to share. It\'s about translating Excel column letters to actu

28条回答
  •  悲&欢浪女
    2020-11-29 22:58

    You can do this in C like this:

    unsigned int coltonum(char * string)
    {
       unsigned result = 0;
       char ch;
    
       while(ch = *string++)
          result = result * 26 + ch - 'A' + 1;
    
      return result;
    }
    

    No error checking, only works for upper case strings, string must be null terminated.

提交回复
热议问题