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 ?
Not very efficient but gets the job done:
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
$endis. 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
demo