Find Excel column number from column title

We have discussed Conversion from column number to Excel Column name. In this post, reverse is discussed.

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

    A -> 1
    B -> 2
    C -> 3
    ...
    Z -> 26
    AA -> 27
    AB -> 28 

Examples:



Input : A
Output : 1

Input : AA
Output : 27

The process is similar to binary to decimal conversion.
For example, to convert AB, we do 26 * 1 + 2.
As another example, to convert CDA, we do
3*26*26 + 4*26 + 1
=>26(3*26 + 4)+ 1
=>26(0*26 + 3*26 +4)+1

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to return title to result
// of excel sheet.
#include <bits/stdc++.h>
  
using namespace std;
  
// Returns resul when we pass title.
int titleToNumber(string s)
{
    // This process is similar to binary-to-
    // decimal conversion
    int result = 0;
    for (const auto& c : s)
    {
        result *= 26;
        result += c  - 'A' + 1;
    }
  
    return result;
}
  
// Driver function
int main()
{
    cout << titleToNumber("CDA") << endl;
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to return title 
// to result of excel sheet.
import java.util.*;
import java.lang.*;
  
class GFG
{
  
// Returns resul when we pass title.
static int titleToNumber(String s)
{
    // This process is similar to 
    // binary-to-decimal conversion
    int result = 0;
    for (int i = 0; i < s.length(); i++)
    {
        result *= 26;
        result += s.charAt(i) - 'A' + 1;
    }
    return result;
}
      
// Driver Code
public static void main (String[] args) 
{
    System.out.print(titleToNumber("CDA"));
}
}
  
// This code is contributed 
// by Akanksha Rai(Abby_akku)

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to return title to result 
# of excel sheet. 
  
# Returns resul when we pass title. 
def titleToNumber(s):
    # This process is similar to binary-to- 
    # decimal conversion 
    result = 0
    for B in range(len(s)): 
        result *= 26
        result += ord(s[B]) - ord('A') + 1
  
    return result; 
  
# Driver function 
print(titleToNumber("CDA")); 
  
# This code contributed by Rajput-Ji

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to return title 
// to result of excel sheet. 
using System;
  
class GFG
{
  
// Returns resul when we pass title. 
public static int titleToNumber(string s)
{
    // This process is similar to 
    // binary-to-decimal conversion 
    int result = 0;
    for (int i = 0; i < s.Length; i++)
    {
        result *= 26;
        result += s[i] - 'A' + 1;
    }
    return result;
}
  
// Driver Code 
public static void Main(string[] args)
{
    Console.Write(titleToNumber("CDA"));
}
}
  
// This code is contributed by Shrikant13

chevron_right



Output:

2133

Time Complexity: O(n) where n is length of input string.

This article is contributed by Sahil Rajput. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up

Article Tags :
Practice Tags :


5


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.