Morse Code Converter- the program is to convert a word or phrase into morse code
ID: 3713492 • Letter: M
Question
Morse Code Converter- the program is to convert a word or phrase into morse code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Morse_Code_Translator
{
public partial class Form1 : Form
{
private char[] chars = {' ', ',', '.', '?', '0', '1', '2', '3', '4' ,'5','6','7','8','9','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'};
private string[] codes = { " ", "--..--", ".---", "..---", "...--","....-",".....","-....","--...",
"---..","----.",".-","-...","-.-.",
"-..",".","..-.","--.","....","..",
".---","-.-",".-..","--","-.","---",
".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
public Form1()
{
InitializeComponent();
}
private void convertButton_Click(object sender, EventArgs e)
{
string englishText = stringTextBox.Text;
string result = "";
int count = 0;
for (int i = 0; i < englishText.Length; i++)
{
for (int j = 0; j < chars.Length; j++)
{
char letter = char.ToUpper(englishText[i]);
if (letter == chars[j])
{
result = result + codes[j];
count++;
}
}
}
if (count == englishText.Length)
{
morseCodeLabel.Text = result;
}
else
{
morseCodeLabel.Text = "Invalid string";
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Programming Lanuage: C# Visual Basic Window Forms
My issue: The code seems to be fine, however I keep getting an error whenever I input a string or word
the error message is - "Unhandled exception has occurred in you application. Index was outside the bounds of the array"
Explanation / Answer
The problem is because the count of both variables char[] chars and string[] codes are not same.
I noticed the count of char[] chars = 40 and string[] codes = 37
And you have used the for loop which runs till the length of variable char[] chars, which means it loops for 40 times, but inside the loop you trying to get the value from variable codes by using the index value.
So when you enter letters x or y or z in text box the error "Index was outside the bounds of the array", why because the index of x, y and z in variable chars is 38, 39 and 40, but you trying to get the value from variable codes using index 38, 39 and 40 where the last index of variable codes is 37. So you get this error
Below i highlighted the code from where the exception throw based on the above explanation
for (int i = 0; i < englishText.Length; i++)
{
for (int j = 0; j < chars.Length; j++)
{
char letter = char.ToUpper(englishText[i]);
if (letter == chars[j])
{
result = result + codes[j];
count++;
}
}
}
I have corrected the values in the variable string[] codes to match the count with char[] chars.
Please copy the below values of string[] codes, now the count of it is 40 (matches with chars count)
private static string[] codes = { " ", "--..--", "·-·-·-", "··--··", "-----", ".---", "..---", "...--","....-",".....","-....","--...",
"---..","----.",".-","-...","-.-.",
"-..",".","..-.","--.","....","..",
".---","-.-",".-..","--","-.","---",
".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.." };
Newly added values is highlighted in bolder text and underlined.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.