+ 2
The Alphabet Project
Let's say I have an array of chars and they are a,b,c,d.... I want the program to return the index of a char. If the user enters d, the output should be 3.
8 Respostas
+ 3
C# has an intended code for that, under the "System.Array" Class.
Try this:
char[] lettersArr = {'a','b','c','d','e'};
Console.WriteLine(Array.IndexOf(lettersArr,'d'));
//it will give 3
but if the result is -1,
that means the element was not found.
Hope that helps! :)
+ 2
Try using this code:
char[] alph = {'a','b','c','d','e'};
char letter=Convert.ToChar(Console.ReadLine());
for (int i=0; i<alph.Length;i++)
{
if (alph[i]==letter)
{ Console.WriteLine(i); }
}
+ 2
Yeah. That's a beautifull solution.
+ 1
i know i just wanna tel you different soloution
+ 1
so we can write a program as different ways
0
char[] Alphabet={'a','b','c','d','e'};
console.write("enter your character:");
var num=char.parse(console.Readline());
switch(num)
{
case'a':
console.writeline(0);
break;
case'b':
console.writeline(1);
break;
case 'c':
console.writeline(2);
break;
case'd':
console.writeline(3);
break;
case'e':
console.writeline(4);
break;
console.writeline(num);
0
Sajjad, that's cool but when you have 26 letters, it's kind of long.
0
Try this:
for(int x=0; x < alphabet.Length; x++) {
if(alphabet[x] == (letter entered by the user) {
return x;
}
}
Tou can use this as a method inside your alphabet class.