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 9165013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:49:01+00:00 2026-06-17T14:49:01+00:00

How can I write a function that gives me number of the character that

  • 0

How can I write a function that gives me number of the character that is passed to it

For example, if the funciton name is GetCharacterNumber and I pass B to it then it should give me 2

GetCharacterNumber("A") // should print 1
GetCharacterNumber("C") // should print 3
GetCharacterNumber("Z") // should print 26
GetCharacterNumber("AA") // should print 27
GetCharacterNumber("AA") // should print 27
GetCharacterNumber("AC") // should print 29

Is it even possible to achieve this ?

  • 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-06-17T14:49:03+00:00Added an answer on June 17, 2026 at 2:49 pm

    Not very efficient but gets the job done:

    function get_character_number($end) 
    {
        $count = 1;
        $char = 'A';
        $end = strtoupper($end);
        while ($char !== $end) {
            $count++;
            $char++;
        }
        return $count;
    }
    
    echo get_character_number('AA'); // 27
    

    demo

    This works because when you got something like $char = 'A' and do $char++, it will change to ‘B’, then ‘C’, ‘D’, … ‘Z’, ‘AA’, ‘AB’ and so on.

    Note that this will become the slower the longer $end is. I would not recommend this for anything beyond ‘ZZZZ’ (475254 iterations) or if you need many lookups like that.

    An better performing alternative would be

    function get_character_number($string) {
        $number = 0;
        $string = strtoupper($string);
        $dictionary = array_combine(range('A', 'Z'), range(1, 26));
        for ($pos = 0; isset($string[$pos]); $pos++) {
            $number += $dictionary[$string[$pos]] + $pos * 26 - $pos;
        }
        return $number;
    }
    
    echo get_character_number(''), PHP_EOL; // 0
    echo get_character_number('Z'), PHP_EOL; // 26
    echo get_character_number('AA'), PHP_EOL; // 27
    

    demo

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

Sidebar

Related Questions

How can I write a function that accepts a variable number of arguments? Is
How do I write a function that can accept unlimited number of parameters? What
I want to write a function in python that can take an arbitrary number
For the following script, how can I write a function that returns all of
I need to write a function that can take an if statement at runtime
I am trying to write a function that can take three parameters: an SqlDataReader,
I'm trying to write a function that can alter the value of a column
I'm trying to write a function that takes multiple arguments, which can come either
I need to write a delegate function that can 'wrap' some while/try/catch code around
I'm wondering how can I write a function: 'a list*int -> 'a list*list that

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.