Find Excel column number from column title
Last Updated :
11 Jul, 2022
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.
column column number
A -> 1
B -> 2
C -> 3
...
Z -> 26
AA -> 27
AB -> 28
Examples:
Input: A
Output: 1
A is the first column so the output is 1.
Input: AA
Output: 27
The columns are in order A, B, ..., Y, Z, AA ..
So, there are 26 columns after which AA comes.
Approach: The process is similar to binary to decimal conversion.
For example, to convert AB, the formula is 26 * 1 + 2.
As another example,
To convert CDA,
3*26*26 + 4*26 + 1
= 26(3*26 + 4) + 1
= 26(0*26 + 3*26 + 4) + 1
So it is very much similar to converting binary to decimal keeping the base as 26.
Take the input as string and the traverse the input string from the left to right and calculate the result as follows:
result = 26*result + s[i] - 'A' + 1
The result will be summation of
Result &= \sum_{i=0}^{n} a[n-i-1]*(26^{(n-i-1)}) .
Implementation:
C++
// C++ program to return title to result
// of excel sheet.
#include <bits/stdc++.h>
using namespace std;
// Returns result 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;
}
Java
// Java program to return title
// to result of excel sheet.
import java.util.*;
import java.lang.*;
class GFG
{
// Returns result 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)
Python3
# Python program to return title to result
# of excel sheet.
# Returns result 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
C#
// C# program to return title
// to result of excel sheet.
using System;
class GFG
{
// Returns result 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
JavaScript
<script>
// JavaScript program to return title
// to result of excel sheet.
// Returns result when we pass title
function titleToNumber(s)
{
// This process is similar to
// binary-to-decimal conversion
let result = 0;
for (let i = 0; i < s.length; i++)
{
result *= 26;
result += s[i].charCodeAt(0) - 'A'.charCodeAt(0) + 1;
}
return result;
}
// Driver Code
document.write(titleToNumber("CDA"));
// This code is contributed by avanitrachhadiya2155
</script>
Complexity Analysis:
- Time Complexity: O(n), where n is length of input string.
- Space Complexity: O(1).
As no extra space is required.
Similar Reads
Excel column name from a given column number MS Excel columns have a pattern like A, B, C, â¦, Z, AA, AB, AC, â¦., AZ, BA, BB, ⦠ZZ, AAA, AAB ..... etc. In other words, column 1 is named "A", column 2 as "B", and column 27 as "AA".Given a column number, the task is to find its corresponding Excel column name.Examples:Input: 26Output: Z Input: 51
8 min read
How to Retrieve Row Numbers in R DataFrame? In this article, we will discuss how to Retrieve Row Numbers in R Programming Language. The dataframe column can be referenced using the $ symbol, which finds its usage as data-frame$col-name. The which() method is then used to retrieve the row number corresponding to the true condition of the speci
2 min read
Select DataFrame Column Using Character Vector in R In this article, we will discuss how to select dataframe columns using character vectors in R programming language. Data frame in use: To extract columns using character we have to use colnames() function and the index of the column to select is given with it using []. The approach is sort of the sa
2 min read
How to get name of dataframe column in PySpark ? In this article, we will discuss how to get the name of the Dataframe column in PySpark. To get the name of the columns present in the Dataframe we are using the columns function through this function we will get the list of all the column names present in the Dataframe. Syntax: df.columns We can a
3 min read
Extract given rows and columns from a given dataframe in R Extraction of given rows and columns has always been one of the most important tasks which are especially required while working on data cleaning activities. In this article, we will be discussing all the sets of commands which are used to extract given rows and columns from a given dataframe in the
4 min read