Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 216349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:33:22+00:00 2026-05-11T18:33:22+00:00

I am trying to create a function which will give me alphabet position when

  • 0

I am trying to create a function which will give me alphabet position when an index is passed. It will be same like how excel shows it’s columns. A…Z, AA,AB…. I wrote the below function to get the results upto Z. It looks like

static string GetColumnName(int index)
{
    const int alphabetsCount = 26;
    if (index <= alphabetsCount)
    {
        int code = (index - 1) + (int)'A';
        return char.ConvertFromUtf32(code);
    }
    return string.Empty;
}

This works fine until ‘Z’. It return ‘A’ if I pass 1 and return ‘B’ if I pass 2 and so on. But, I am not able to figure out how will I get AA when I pass 27 to this function. I guess I need a recursive method to find it.

Any inputs to this problem will be great!

Edit

This is suggested by Tordek. But his code will fail in numbers like 52, 78 etc. Added workaround for that and here is the final working code.

static string GetColumnName(int index)
{
    const int alphabetsCount = 26;

    if (index > alphabetsCount)
    {
        int mod = index % alphabetsCount;
        int columnIndex = index / alphabetsCount;

        // if mod is 0 (clearly divisible) we reached end of one combination. Something like AZ
        if (mod == 0)
        {
            // reducing column index as index / alphabetsCount will give the next value and we will miss one column.
            columnIndex -= 1;
            // passing 0 to the function will return character '@' which is invalid
            // mod should be the alphabets count. So it takes the last char in the alphabet.
            mod = alphabetsCount;
        }
        return GetColumnName(columnIndex) + GetColumnName(mod);
    }
    else
    {
        int code = (index - 1) + (int)'A';
        return char.ConvertFromUtf32(code);
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T18:33:22+00:00Added an answer on May 11, 2026 at 6:33 pm

    Any recursive function can be converted into an equivalent iterative one. I find it always easy to think recursively first:

    static string GetColumnName(int index)
    {
        const int alphabetsCount = 26;
    
        if (index > alphabetsCount) {
            return GetColumnName(index / alphabetsCount) + GetColumnName(index % alphabetsCount);
        } else {
            int code = (index - 1) + (int)'A';
            return char.ConvertFromUtf32(code);
        }
    }
    

    Which can be simple converted into:

    static string GetColumnName(int index)
    {
        const int alphabetsCount = 26;
        string result = string.Empty;
    
        while (index > 0) {
            result = char.ConvertFromUtf32(64 + (index % alphabetsCount)) + result;
            index /= alphabetsCount;
        }
    
        return result;
    }
    

    Even so, listen to Joel.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 200k
  • Answers 200k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer According to the documentation for random.seed: If x is omitted… May 12, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer Set Delayed::Job.destroy_failed_jobs = false in an initializer, then take a… May 12, 2026 at 7:59 pm
  • Editorial Team
    Editorial Team added an answer Just saw that this was released today: The Facebook SDK… May 12, 2026 at 7:59 pm

Related Questions

I am doing some work to track down a perceived bug in a git
I am rather new to flex and am trying to figure out how to
I have an application that I am writing that modifies data on a cached
I am trying to create a point system in my program similar to stack

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.