The Alphabet Project | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

18th Jun 2016, 2:46 AM
Mitiko
Mitiko - avatar
8 Answers
+ 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! :)
19th Jun 2016, 2:11 PM
erwinmesi
erwinmesi - avatar
+ 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); } }
18th Jun 2016, 4:40 AM
TatoALRC
TatoALRC - avatar
+ 2
Yeah. That's a beautifull solution.
18th Jun 2016, 4:53 AM
Mitiko
Mitiko - avatar
+ 1
i know i just wanna tel you different soloution
20th Jun 2016, 6:31 PM
sajjad
sajjad - avatar
+ 1
so we can write a program as different ways
20th Sep 2016, 1:46 AM
venkata veerendra manikumar kancharla
venkata veerendra manikumar kancharla - avatar
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);
20th Jun 2016, 11:07 AM
sajjad
sajjad - avatar
0
Sajjad, that's cool but when you have 26 letters, it's kind of long.
20th Jun 2016, 6:28 PM
Mitiko
Mitiko - avatar
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.
23rd Jun 2016, 1:46 AM
William
William - avatar