First things first - I know you are a beginner, but...there are some things you can do to improve that, and make it a lot more readable.
Start by looking at it, and see if you can spot anything you do several times...like your
if
test for example! My word, but that's a bit of an eyesore and probably hard to type as well.
So let's move that to a separate method:
private bool IsAlpha(char c)
{
return c == 'A' || c == 'B' || c == 'C' || c == 'D' || c == 'E' || c == 'F' || c == 'G' || c == 'H' || c == 'I' || c == 'J' || c == 'K' || c == 'L' || c == 'M' || c == 'N' || c == 'O' || c == 'P' ||
c == 'Q' || c == 'R' || c == 'S' || c == 'T' || c == 'U' || c == 'V' || c == 'W' || c == 'X' || c == 'Y' || c == 'Z';
}
Now, we can use this in your code:
if (b == 32)
{
Console.Write(" "+b);
}
if (IsAlpha(c))
{
a = Convert.ToInt32(str[i] - 64);
if (a < 10)
That's a bit more readable!
It's odd really, because you are relying on 'B' being one bigger than 'A' in your conversion, but you ignored that when you tested for "Is this between 'A' and 'Z'?" - you could have rewritten IsAlpha as:
private bool IsAlpha(char c)
{
return c >= 'A' && c <= 'Z';
}
And it would have the same effect!
As it happens, we don't need to write our own IsAlpha method: .NET provides us with one.
We can just say:
if (Char.IsLetter(c))
And we get the same effect (but with a much better range of values as it supports accents as well)
Ah, but I can hear you saying already: "But I need upper and lowercase differently!"
Well, yes, you do - but there again, no, you don't.
Because the way you are converting characters to integers (and vice versa) could be simpler as well.
When you convert 'A' to a number, you are translating it to "01" and you do the same with 'a' - it's a "01" as well. So why not just convert all lowercase character to uppercase? Then you only have to look at one range of characters!
And .NET does that for us as well! There are two ways: the whole string can become uppercase with string.ToUpper, or you can do it character by character with char.ToUpper. The whole string is easier!
Now, let's make it easier to read again, by taking the conversion code out into another method:
private int ToNumber (char c)
{
return (int) c - (int) 'A';
}
'A' is just your constant value 64, but left as a character so the code is more obvious.
The next change I'd make is to not use a
for
loop - I'd use a
foreach
instead.
This makes the whole code a lot simpler, and much easier to read:
str = str.ToUpper();
foreach (char each in str)
{
if (char.IsLetter(each))
{
int value = (int)each - (int)'A' + 1;
Console.Write("{0:00} ", value);
}
}
Console.WriteLine();
Replaces your whole code for the first segment...and if you make this a method, you can just all it and pass your "normal" string, reverse that, and pass the reversed string to the same method!
I'm going to stop there, as I't probably enough for you at the moment!
Have a try, and a think, and see if you can apply any of this to your "convert it back to letters" part. I think you probably can!