Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Morse code is a code where each letter of the English alphabet, each digit, and

ID: 3725742 • Letter: M

Question

Morse code is a code where each letter of the English alphabet, each digit, and various punctuation characters are represented by a series of dots and dashes. Table 9-14 on page 612 of your textbook provides details on conversion from character to code. Write a program that asks the user to enter a string, and then converts that string to Morse code. Use hyphens for dashes and periods for dots.

Input Validation: Do not accept unreasonable/junk values, or negative values, which must be rejected and asked for another one.

Explanation / Answer

/*

References:

http://en.wikipedia.org/wiki/Morse_code

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 --..

0 -----

1 .----

2 ..---

3 ...--

4 ....-

5 .....

6 -....

7 --...

8 ---..

9 ----.

Fullstop .-.-.-

Comma --..--

Query ..--..

Space /

Period [.] · - · - · -

Comma [,] - - · · - -

Question mark [?] · · - - · ·

Apostrophe ['] · - - - - ·

Exclamation mark [!] - · - · - -

Slash [/] - · · - ·

Parentheses ( ) - · - - · -

Ampersand [&] · ···

Colon [:] - - - · · ·

Semicolon [;] - · - · - ·

Double dash [=] - · · · -

Fraction bar - · · - ·

Hyphen [-] - · · · · -

Underscore [_] · · - - · -

Quotation mark ["] · - · · - ·

"@" (commat) · - - · - ·

*/

/* */

#include<stdio.h>

#include<conio.h>

#include<string.h>

#include<process.h>

void main()

{

char str[25],str1[100];

fflush(stdin);

printf("Enter any String to Convert into Morse Code :: ");

gets(str);

int j=0;

for(int i=0;i<=strlen(str);i++)

{

switch(toupper(str[i]))

{

case 'A':

str1[j++]='.';

str1[j]='-';

break;

case 'B':

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j]='.';

break;

case 'C':

str1[j++]='-';

str1[j++]='.';

str1[j++]='-';

str1[j]='.';

break;

case 'D':

str1[j++]='-';

str1[j++]='.';

str1[j]='.';

break;

case 'E':

str1[j]='.';

break;

case 'F':

str1[j++]='.';

str1[j++]='.';

str1[j++]='-';

str1[j]='.';

break;

case 'G':

str1[j++]='-';

str1[j++]='-';

str1[j]='.';

break;

case 'H':

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j]='.';

break;

case 'I':

str1[j++]='.';

str1[j]='.';

break;

case 'J':

str1[j++]='.';

str1[j++]='-';

str1[j++]='-';

str1[j]='-';

break;

case 'K':

str1[j++]='-';

str1[j++]='.';

str1[j]='-';

break;

case 'L':

str1[j++]='.';

str1[j++]='-';

str1[j++]='.';

str1[j]='.';

break;

case 'M':

str1[j++]='-';

str1[j]='-';

break;

case 'N':

str1[j++]='-';

str1[j]='.';

break;

case 'O':

str1[j++]='-';

str1[j++]='-';

str1[j]='-';

break;

case 'P':

str1[j++]='.';

str1[j++]='-';

str1[j++]='-';

str1[j]='.';

break;

case 'Q':

str1[j++]='-';

str1[j++]='-';

str1[j++]='.';

str1[j]='-';

break;

case 'R':

str1[j++]='.';

str1[j++]='-';

str1[j]='.';

break;

case 'S':

str1[j++]='.';

str1[j++]='.';

str1[j]='.';

break;

case 'T':

str1[j]='-';

break;

case 'U':

str1[j++]='.';

str1[j++]='.';

str1[j]='-';

break;

case 'V':

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j]='-';

break;

case 'W':

str1[j++]='.';

str1[j++]='-';

str1[j]='-';

break;

case 'X':

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j]='-';

break;

case 'y':

str1[j++]='-';

str1[j++]='.';

str1[j++]='-';

str1[j]='-';

break;

case 'Z':

str1[j++]='-';

str1[j++]='-';

str1[j++]='.';

str1[j]='.';

break;

case '0':

str1[j++]='-';

str1[j++]='-';

str1[j++]='-';

str1[j++]='-';

str1[j]='-';

break;

case '1':

str1[j++]='.';

str1[j++]='-';

str1[j++]='-';

str1[j++]='-';

str1[j]='-';

break;

case '2':

str1[j++]='.';

str1[j++]='.';

str1[j++]='-';

str1[j++]='-';

str1[j]='-';

break;

case '3':

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j++]='-';

str1[j]='-';

break;

case '4':

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j]='-';

break;

case '5':

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j]='.';

break;

case '6':

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j]='.';

break;

case '7':

str1[j++]='-';

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j]='.';

break;

case '8':

str1[j++]='-';

str1[j++]='-';

str1[j++]='-';

str1[j++]='.';

str1[j]='.';

break;

case '9':

str1[j++]='-';

str1[j++]='-';

str1[j++]='-';

str1[j++]='-';

str1[j]='.';

break;

case '.':

str1[j++]='.';

str1[j++]='-';

str1[j++]='.';

str1[j++]='-';

str1[j++]='.';

str1[j]='-';

break;

case ',':

str1[j++]='-';

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j++]='-';

str1[j]='-';

break;

case ':':

str1[j++]='-';

str1[j++]='-';

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j]='.';

break;

case '?':

str1[j++]='.';

str1[j++]='.';

str1[j++]='-';

str1[j++]='-';

str1[j++]='.';

str1[j]='.';

break;

case '-':

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j]='-';

break;

case ';':

str1[j++]='-';

str1[j++]='.';

str1[j++]='-';

str1[j++]='.';

str1[j++]='-';

str1[j]='.';

break;

case '"':

str1[j++]='.';

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j++]='-';

str1[j]='.';

break;

case '+':

str1[j++]='.';

str1[j++]='-';

str1[j++]='.';

str1[j++]='-';

str1[j]='.';

break;

case '/':

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j++]='-';

str1[j]='.';

break;

case '&':

str1[j++]='.';

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j]='.';

break;

case '$':

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j]='-';

break;

case '@':

str1[j++]='.';

str1[j++]='-';

str1[j++]='-';

str1[j++]='.';

str1[j++]='-';

str1[j]='.';

break;

case '=':

str1[j++]='-';

str1[j++]='.';

str1[j++]='.';

str1[j++]='.';

str1[j]='-';

break;

case ' ':

str1[j]=' ';

break;

}

j++;

}

str1[j-1]='';

puts(str1);

getch();

}

OutPut ::

Enter any String to Convert into Morse Code :: M O R S E C O D E
-- --- .-. ... . -.-. --- -.. .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote