As a guide, I'd probably start with a Dictionary<char, string> to provide the mapping from letters to Morse code. You can then loop through a string directly, as it is an IEnumerable<char>, or call ToCharArray on it. Once you have put that information to use, you can show us your code if you encounter any actual issues. If you haven't written any code, you haven't encountered any issues yet.
Dictionary<char, string> Morze = new Dictionary<char, string>()
{
{'A',".- "},
{'B', "-... "},
{'C',"-.-. "},
{'D'," -.. "},
{'E',"."},
{'F',"..-. "},
{'G',"--. "},
{'H',"...."},
{'I',".."},
{'J',".---"},
{'K',"-.-"},
{'L',".-.."},
{'M',"--"},
{'N',"-."},
{'O',"---"},
{'P',".--."},
{'Q',"--.-"},
{'R',".-."},
{'S',"..."},
{'T',"-"},
{'U',"..-"},
{'V',"...-"},
{'W',".--"},
{'X',"-..-"},
{'Y',"-.--"},
{'Z',"--.."},
{'1',".----"},
{'2',"..--- "},
{'3',"...-- " },
{'4',"....- " },
{'5',"..... " },
{'6',"-.... " },
{'7',"--... " },
{'8',"---.. " },
{'9',"----. " },
{'0',"----- " },
{'/',"-..-. "},
{'+',".-.-. " },
{'=',"-...-" },
{'.',".-.-.-" },
{',',"--..--" },
{'?',"..--.." },
{'(',"-.--." },
{')',"-.--.-" },
{'-',"-....-" },
{'"',".-..-." },
{'_',"..--.-" },
{':',"---..." },
{';',"-.-.-." },
{'$',"...-..-"}
};
Console.Write("Input a name: ");
string name = Console.ReadLine();
Console.WriteLine(Morze[Convert.ToChar(name)]);
I don't understand how IEnumerable<char>, or call ToCharArray works is this right?It seems like you read the Dictionary part, implemented that and then ignored the rest. Go back and read my previous post again and this time use the rest of the information provided.
He was referring to you using a loop, where's your loop in your new code?I don't understand how IEnumerable<char>, or call ToCharArray works is this right?
![]()
IEnumerable Interface (System.Collections)
Exposes an enumerator, which supports a simple iteration over a non-generic collection.docs.microsoft.com
highlighted
text is. Upon doing so, it should be very clear to you what you're being advised to do once you follow the directions you are given on your topic and in the documentation you are looking into. Plus, almost all MSDN pages, comes with very helpful code examples, including descriptive and very elaborate explanations describing the usage for whatever it is you're needing to do further researching on. static void Task3()
{
string word = "3.Uzdevums";
Console.ForegroundColor = ConsoleColor.Green;
Console.SetCursorPosition((Console.WindowWidth - word.Length) / 2, Console.CursorTop);
Console.WriteLine(word);
Console.ForegroundColor = ConsoleColor.DarkCyan;
Console.WriteLine();
//////////////////////
//////////////////////
Random rnd = new Random();
// Masīvs char
char[] chars = {
' ', 'a', 'b', 'c', 'd',
'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's',
't', 'u', 'v', 'w', 'x',
'y', 'z', '1', '2', '3',
'4', '5', '6', '7', '8',
'9', '0' };
// masīvs string
string[] morze = {
" ", ". -", "- . . .", "- . - .", "- . .",
".", ". . - .", "- - .", ". . . .", ". .",
". - - -", "- . -", ". - . .", "- -", "- .",
"- - -", ". - - .", "- - . -", ". - .", ". . .",
"-", ". . -", ". . . -", ". - -", "- . . -", "- . - -",
"- - . .", ". - - - -", ". . - - -", ". . . - -", ". . . . -",
". . . . .", "- . . . .", "- - . . .", "- - - . .",
"- - - - .", "- - - - -" };
// Tukšu string definēšana lietotāja ievadītā vārda un translācijas glabāšanai.
string Input = "";
string translation = "";
// Tulkošana
Console.Write("Input text, that'll be translated: ");
Input = Console.ReadLine();
Input = Input.ToLower();
for (int x = 0; x < Input.Length; x++)
{
for (int y = 0; y < chars.Length; y++)
{
if (Input[x] == chars[y])
{
translation += morze[y];
translation += " ";
break;
}
}
}
char[] Array = translation.ToCharArray();
for (int i = 0; i < translation.Length; i++)
{
if (Array[i] == '.')
{
Console.Beep(1000, 100);
}
if (Array[i] == '-')
{
Console.Beep(1000, 800);
}
if (Array[i] == ' ')
{
Thread.Sleep(100);
}
}
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write($"Translation: {translation}");
Console.WriteLine();
}
}
rnd
variable on line 14?chars
and morze
was the way to do things because that was the only thing they had with most of the commonly available programming languages back then. Nowadays we have learned to keep related information together in struct
's and class
's or special data structures. In this case when you are mapping one value to another, a Dictionary<>
is the best data structure -- and this is what was suggested very early on in this thread.string input = Console.ReadLine();
string translation = string.Empty;
foreach (char ch in input)
{
int index = Array.IndexOf(chars, char.ToLower(ch));
// If invalid characters may be present, allow for index to be -1.
translation += morze[index];
}