convert every char to morse string?

himlastar

Member
Joined
Mar 16, 2019
Messages
10
Programming Experience
Beginner
User inputs a word and it converts it to Morzes code!?
How can I convert every char to string?
Like I write - " Programm "
And it would say " .--. .-. " and so on?
Can anyone help me?
 
Yes we can help you if you show us what you've currently got, and tell us what problems you have encountered. We are not a code writing service. We don't offer that kind of help.
 
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.
 
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.

I came up with this code, but I can only input one character and it only displays one, I need a full word like - "HELLO"
How could I do that!?

C#:
    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)]);
 
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.
 
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.
I don't understand how IEnumerable<char>, or call ToCharArray works is this right?

 
I specifically said that you can loop over the string itself or you can call ToCharArray. Do you not know how arrays work either? The point of a loop is to perform an operation repeatedly. You have multiple chars in that string or an array created from it so that sounds like a perfect match.

Programming doesn't exist in a vacuum. Code is just an implementation of an algorithm. You should already know what that algorithm is before you try to write the code. If you had to do this using pen and paper, how would you do it? That's how you do it in code. Think about the process, write it down if you need to, then write code to implement those steps. Depending on the words you use, it's virtually code already, e.g. "for each character in the input, find the corresponding Morse code and add it to the output". If I'm not mistaken, C# has foreach loops. As it happens, a C# foreach specifically works by enumerating the items in an object that implements the IEnumerable interface. It all seems to be coming together.
 
@himlastar just a little tip while you're here on csharpforums. We understand that you may be new to the language, and we all make exceptions for this when helping people like you. But when someone on this forum puts something in italic, (as almost all of us do when we are trying to point out something specific or when giving a tip.) It's generally implied that you either search the interwebs or typically MSDN : Search for whatever the italic or 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. (y)
 
C#:
  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();
        }
    }
 
What's the point of the rnd variable on line 14?

Yes, back in the 1950-60's using parallel arrays like you are doing with your 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.

And as a minor quibble: the duration of a dash is supposed to be three times the length of a duration of a dot. It's one of the few things I still recall when I was trying to learn Morse code in Cub Scouts.
 
Last edited:
So you got the Dictionary part right to begin with but you broke it and now you have a char array but you can't use it properly. You should go back to the Dictionary but, if you want to use concurrent arrays, at least use them properly. The whole point is that the elements at the same index correspond, so you should be using that relationship:
C#:
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];
}
 
Back
Top Bottom