❔ Alphabetic assign to number
Im pretty new to C# and i want to get
01 - a
02 - b
03 - c
04 - d
.... and other
30 Replies
How far do you want this to extend?
Like do you only need up to z?
yes
from A to Z
this is what i got to get A to 01, B to 02
i mean i dont know how to do it
when in doubt, use a dictionary/hashmap
I see so you're trying to package the alphabet as a string of numbers
With the numbers padded with zeros
Could I just ask what you plan to do with this string, @justkill2? I ask because it might be that this isn't necessary for you to do in the long run
I need to convert to string bcs im putting in textBox.Text
which need to be in string
No like why do you want to change the string from textBox1.Text into numbers in the first place?
bcs im working on my school project, where i crypt and encrypt text user will put in
That's a good a reason as any
So in that case I think your solution is close to the right one, but there are some simplifications that could be made
What happens if the input has a space in it?
Or can you assume it won't?
no it wont
Ok so I've come up with a fairly simple solution but as you're meant to learn I'll help you come to it yourself haha
Have you done
foreach
loops yet?
Also have you done StringBuilder
?yes
Grand. You should be able to make
encoded
as a StringBuilder
rather than a string
. At this scale it doesn't really matter either way but it's good practice haha. Instead of using a for
loop I'd recommend using a foreach ... in
loop because it means you can get each char directly rather than having to index the arrayokay i will try that
You can also use a string feature called
PadLeft
to add the zeros in front when they're neccesary
Something like idx.ToString().PadLeft(2, '0');
would work
In that way it'd only put in the zeros if there's space to do sothank you im finally learning more stuff here than we do at school
Ikr I definitely had that experience
I'd also recommend moving the indentation for the code from lines 2 onwards all the way back to the left - indentation is meant to tell the programmer what the scope is (have you done scope yet?) and this indentation misleads the reader
no we havent done scope yet
Long story short, scope is where you keep track of which variables you get to use
The idea is that after a while you're done with your variables and you want them to go away
And the program gets the hint and dumps them for you (garbage collection)
For example
Here then line 4 can see both
myVar
and i
ye
Because the scope of the for loop is inside the scope of the wider code
But the last line cannot see
i
anymore because you left the for loop and so the i
was thrown away
Csharp would get upset at you if you tried to run this code
Most languages use {
and }
to tell you that you're entering or leaving a scope
The idea with the indentation is that you can guess what scope you're in by just looking at how many spaces there are in front of the code
For example in the for loop I just wrote you can tell that i
is visible inside the for loop but not outside because the for loop code is indented by 4 spaces
But if I did it the other way around
Or if I did this
```csharp
var myVar = 0;
for (int i = 0; i < 10; i++) {
Console.WriteLine(myVar, i);
}
Console.WriteLine(myVar, i);
```
Then the reader might have to think harder to figure out what's going on
Because you're not offering them any hints haha
So for your original code I'd recommend writing it like this
```csharp
string word = textBox1.Text;
char[] array = word.ToCharArray();
string encoded = "";
for (int i = 0; i < array.Length; i++)
{
int idx = char.ToUpper(array[i]) - 64;
string translate = idx.ToString();
if (idx < 10) translate = "0" + idx.ToString();
encoded = encoded + translate;
}
//MessageBox.Show(encoded);
textBox2.Text = encoded;
```
Because in your original code it looks as though I won't be able to use
array or
encoded` once we get to the last line - just because of what the indentation tells me
Am I making sense or am I talking nonsense?ye its making sense
That's great news
Ok so I've made a couple of proposed changes - could I see your original code updated with those changes?
alright gime sec
i tried it the ghetto way to make it
without you
but now i will try it with ur hints
I'll keep adding hints until we're both satisfied haha
haha
Use the fact that chars are actually integers
Angius#1586
REPL Result: Success
Console Output
Compile: 627.131ms | Execution: 98.942ms | React with ❌ to remove this embed.
Like so
(
:D2
just means to pad the number to at least two digits)Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.