C
C#2y ago
justkill2

❔ 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
Thinker
Thinker2y ago
How far do you want this to extend? Like do you only need up to z?
justkill2
justkill22y ago
yes from A to Z
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;
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;
this is what i got to get A to 01, B to 02 i mean i dont know how to do it
friedice
friedice2y ago
when in doubt, use a dictionary/hashmap when
rotor_
rotor_2y ago
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
justkill2
justkill22y ago
I need to convert to string bcs im putting in textBox.Text which need to be in string
rotor_
rotor_2y ago
No like why do you want to change the string from textBox1.Text into numbers in the first place?
justkill2
justkill22y ago
bcs im working on my school project, where i crypt and encrypt text user will put in
rotor_
rotor_2y ago
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?
justkill2
justkill22y ago
no it wont
rotor_
rotor_2y ago
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?
justkill2
justkill22y ago
yes
rotor_
rotor_2y ago
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 array
justkill2
justkill22y ago
okay i will try that
rotor_
rotor_2y ago
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 so
justkill2
justkill22y ago
thank you im finally learning more stuff here than we do at school
rotor_
rotor_2y ago
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
justkill2
justkill22y ago
no we havent done scope yet
rotor_
rotor_2y ago
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
var myVar = 0;

for (int i = 0; i < 10; i++) {
Console.WriteLine(myVar, i);
}

Console.WriteLine(myVar, i);
var myVar = 0;

for (int i = 0; i < 10; i++) {
Console.WriteLine(myVar, i);
}

Console.WriteLine(myVar, i);
Here then line 4 can see both myVar and i
justkill2
justkill22y ago
ye
rotor_
rotor_2y ago
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
var myVar = 0;

for (int i = 0; i < 10; i++) {
Console.WriteLine(myVar, i);
}

Console.WriteLine(myVar, i);
var myVar = 0;

for (int i = 0; i < 10; i++) {
Console.WriteLine(myVar, i);
}

Console.WriteLine(myVar, i);
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?
justkill2
justkill22y ago
ye its making sense
rotor_
rotor_2y ago
That's great news Ok so I've made a couple of proposed changes - could I see your original code updated with those changes?
justkill2
justkill22y ago
alright gime sec i tried it the ghetto way to make it without you but now i will try it with ur hints
rotor_
rotor_2y ago
I'll keep adding hints until we're both satisfied haha
justkill2
justkill22y ago
haha
Angius
Angius2y ago
Use the fact that chars are actually integers
MODiX
MODiX2y ago
Angius#1586
REPL Result: Success
for (int ch = 'A'; ch <= 'Z'; ch++)
{
Console.WriteLine($"{(char)ch} - {(ch + 1 - 'A'):D2}");
}
for (int ch = 'A'; ch <= 'Z'; ch++)
{
Console.WriteLine($"{(char)ch} - {(ch + 1 - 'A'):D2}");
}
Console Output
A - 01
B - 02
C - 03
D - 04
E - 05
F - 06
G - 07
H - 08
I - 09
J - 10
K - 11
L - 12
M - 13
N - 14
O - 15
P - 16
Q - 17
R - 18
S - 19
T - 20
U - 21
V - 22
W - 23
X - 24
Y - 25
Z - 26
A - 01
B - 02
C - 03
D - 04
E - 05
F - 06
G - 07
H - 08
I - 09
J - 10
K - 11
L - 12
M - 13
N - 14
O - 15
P - 16
Q - 17
R - 18
S - 19
T - 20
U - 21
V - 22
W - 23
X - 24
Y - 25
Z - 26
Compile: 627.131ms | Execution: 98.942ms | React with ❌ to remove this embed.
Angius
Angius2y ago
Like so
Thinker
Thinker2y ago
(:D2 just means to pad the number to at least two digits)
Accord
Accord2y ago
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.