Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# C#4.0
I want c# code (function) for bellow formats.
Where A=1, B=2, C=3, D=4, ......Z=26
AA= 27, AB=28,AC=29...... AZ=52
BA=53, BB=54,BC=55, ......BZ=78
And so on...
 
print output like.
 
Alphabet A, Number is 1
Alhphabet B, Number is 2
 
And so on...
Posted 13-Aug-12 5:29am
Edited 13-Aug-12 5:30am
v2
Comments
Richard MacCutchan at 13-Aug-12 11:32am
   
This looks like a homework question; I suggest you go back to your notes and review some of the basics. Hint: try doing this with pencil and paper and work it out from there.
Kenneth Haugland at 13-Aug-12 11:41am
   
or excel work :)
riteshsingh_jsr at 13-Aug-12 11:51am
   
Yes, this question asked to me in an interview...
AshishChaudha at 13-Aug-12 11:40am
   
have you tried anything???
Wes Aday at 13-Aug-12 11:41am
   
You will learn a lot more by doing your own homework yourself.
riteshsingh_jsr at 13-Aug-12 11:52am
   
ok... thanks...
CIDev at 13-Aug-12 12:28pm
   
Try writing some code for this then if you have a problem come back with a specific question.
Clifford Nelson at 13-Aug-12 13:32pm
   
This is acutally a hard question if one does it algorithmically. Sort of base 27, but sort of not.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

public static string ConvertAlpha(int value)
{
    const int a = (int)'A';
    value = value - 1;
    var returnValue = new StringBuilder();
    while (value > -1)
    {
        var remainder = value % 26;
        returnValue.Insert(0, (char)(a + remainder));
        value = value / 26  - 1;
    }
    return returnValue.ToString();
}
 
public static int ConvertNumber(string value)
{
    const int a = (int)'A' - 1;
    int returnValue = 0;
    foreach (var character in value.ToUpper())
    {
        returnValue *= 26;
        returnValue += (int)character - a;
    }
    return returnValue;
}
 

Seems to work right.
  Permalink  
v2
Comments
riteshsingh_jsr at 14-Aug-12 0:16am
   
Thanks Clifford... I will try your code...
CIDev at 14-Aug-12 9:29am
   
Good answer, you get a 5 :)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

you can treat it as base 26 format and depending on the position of the alphabet you can use 26*alphabet at 2nd position+ alphabet at 1nd position and so on...
  Permalink  
Comments
riteshsingh_jsr at 13-Aug-12 11:52am
   
I will try... Thanks for reply...
learner'sbug at 13-Aug-12 12:42pm
   
np...
learner'sbug at 13-Aug-12 12:42pm
   
who is voting it down and why ??
Clifford Nelson at 13-Aug-12 15:29pm
   
I voted it down because it is wrong. It is not a straight base 26 because there is also the blank. You did not test it. If you can create a example that uses straight base 26 I will correct my vote. Wish it was so simple. Also, did not provide the code. You shot from the hip, and missed by a mile. Next time spend a little time testing your concept before posting.
learner'sbug at 13-Aug-12 16:02pm
   
There is no mention of blank in the question.its from A-Z and therefore base 26.and Is it mandatory to have code in the answer ? I don't think so.
Clifford Nelson at 13-Aug-12 16:35pm
   
So I did not explain it so well. Best thing is for you to try your code. Do some work. that will prove it. If you can post code that works with simple base 26, as I said (and you ignored), I will give you a five.
 
PS. My code will work with three letters, and I posted both the coversion to and from.
Clifford Nelson at 13-Aug-12 17:14pm
   
Since obviously somebody has also been mislead (voted you a 5 when you are wrong and don't know it), I will explain better. In a base system you have a 0 and however many numbers as you specify. If A is zero, then you would have a never ending set of A's when you have any mod. So A is one. Then what is AA. You would think obviously 27, but it is not since Z is 26 and then A? (where ? is the zero value would be 27, and AA should be 28. If you take A as zero instead, then after Z would increment to BA (1 = B, 0 = A), that's not right either.
learner'sbug at 14-Aug-12 9:19am
   
If he want A to be one, why are you talking about 0 based base system. A base system act like how you define it. I f A is 1 and Z is 26 then next number would be AA not A0 because 0 is not define in our base 26 system...so AA would be 1*26^1+1=27 not 28...
Clifford Nelson at 14-Aug-12 11:14am
   
Have you tried coding your solution yet????
learner'sbug at 14-Aug-12 13:02pm
   
private static int ConvertToBase26(string input)
{
const int i = (int)'A';
int returnInt = 0;
foreach (Char c in input)
{
returnInt *= 26;
returnInt += (int)c - i+1;
}
 
return returnInt;
}
Clifford Nelson at 14-Aug-12 14:09pm
   
Now try to solve the question instead of convertion back. That is basically the same code I showed above except one minor difference. He did not want the converton from a string to a number, he wanted a number to a string. That one is harder.
learner'sbug at 14-Aug-12 14:25pm
   
"He did not want the converton from a string to a number, he wanted a number to a string.",He just wanted to print Alphabet A, Number is 1
Alhphabet B, Number is 2..you can do it using any method ..either using string to number or from number to string..and also my code prints AA = 27..that is what was asked in the question...My answer was clearly telling him to do what was required as per the question..so don't know why do you keep saying that it is base 27.
Clifford Nelson at 14-Aug-12 17:29pm
   
OK gave you a 5 as promised
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

How about using Math.DivRem(...)?
E.g.
public static string ToExcelName(int num)
{
    List<char> digits = new List<char>();
    do
    {
        int rem;
        num = Math.DivRem(num, 26, out rem);
        digits.Add(Convert.ToChar(65+rem));
    } while (num != 0);
    return string.Join(null, digits.Reverse<char>());
}
 
Cheers
Andi
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,034
1 Sergey Alexandrovich Kryukov 4,838
2 Peter Leow 3,129
3 Maciej Los 3,079
4 DamithSL 2,465


Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100