Monday, March 23, 2009

Shanti C# -> Morse Code version2

Shanti gave me this as homework. It has basically the same specs as per the question discussed in the previous entry, except that the user should type in text (alphanumerics), and the text should be translated automatically to Morse code.

The specs I gathered are:

  1. Type text in the combo box, and Morse code of the text being typed should be displayed.
  2. I assume that she wants the translation to be dynamic (On key press). Another solution is to use some static textboxes and buttons, and on click of the button, the text is translated to morse code. But, sounds to easy :), so I'll try the dynamic one.
The final product looks as follows:



Actually, I am using the same code from the previous Morse version 1 code (Declare and Initialise string arrays. Also populate the combo and the list boxes, and associate both boxes with the indices (Note: we don't say 'indexes') of the respective arrays. Refer to previous post on how to do it.) .

Below is the procedure to get this to work, considering the fact that this is an enhancement to Morse version 1.


Let's try to figure out how this should work.
We want the program to detect any input from the user, and translate the input text to Morse.
The input text is entered in the combo box, and a label will be used (see picture, right hand side of dialogbox) to display the Morse code.

  1. We deduce that the changes are made in the Event cmbText_KeyDown.
  2. In this event, we need to capture the text being entered by the user.
  3. We want to compare the text entered, CHARACTER BY CHARACTER, with the values in the combobox.
  4. What if the user enters lowercase alphabets? Remember that the array that contains them are all in uppercase. We will need to do the comparison with uppercases only.

The code is as follows:

private void cmbText_KeyDown(object sender, KeyEventArgs
e)
{
string sLabel = "";
string sTextEntered = cmbText.Text.ToUpper();//Convert
the text entered by user to uppercases, and put it in
the sTextEntered string


for (int i = 0; i < sTextEntered.Length; i++)//We
need to loop for all characters, one by one, entered by
the user
{
myChar = sTextEntered[i];//myChar is a char
variable, declared in the class section as being a
global variable. It contains the character in
string at position [i]


for (int j = 0; j < sMorse.Length; j++) //We
make use of a nested for loop, using j as counter,
because we need to compare each character entered
in the combo with all the elements present in the
alphanumerics array (0-9, A...Z)
{
if (myChar == char.Parse(sText[j]))//if
the character in the textbox matches the character
in the alphanumeric array, we have a catch

{
sLabel += sMorse[j] + " ";//on
catch, we put the corresponding Morse code to
another string sLabel

break;//When we have a catch, we
do not want to continue the for loop, so we
break/exit to save time

}
}
}
lblMorse.Text = sLabel;//We put the resulting
Morse into the label. Done!

}

4 comments:

  1. At first, I couldn't understand the below code for extracting one character from string.

    myChar = sTextEntered[i];

    I didn't know that string type can be indexed just like arrays, so, I was using substring method.
    e.g.
    myStr = sTextEntered.Substring(i, 1);

    I think your code is best way.
    Thanks a lot for good tips!

    ReplyDelete
  2. To be really honest with you, my first thought was to use Susbstring as well!

    However, in C#, better and easier ways (methods) of using strings are present because strings are so popular nowadays(Davis & Spar, 'C# for Dummies', Wiley p.53). So why not use them? :)

    Both methods work fine, it is just a matter of personal choice.

    ReplyDelete
  3. Almost forgot. A string is just an array of characters which means you can use square brackets like you would with an array to access any character in a string. There! :D

    ReplyDelete
  4. It is interesting...and above comment is easy to understand. Thanks.

    ReplyDelete