Code:
/*
References:
http://en.wikipedia.org/wiki/Morse_code
http://www.techonthenet.com/ascii/chart.php
http://cprogramming.com
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>
void toUpperCase(char s[]);
char *toMorse(char s[]);
int main(int argc, char *argv[])
{
char *morse;
char s[]="sos call 911";
toUpperCase(s);
morse = toMorse(s);
printf("%s",morse);
return 0;
}
char *toMorse(char s[])
{
static char x[1024];
char c[][37] = {
".-", /* 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*/
};
int i=0;
while(s[i]!='\0')
{
if(s[i]>=65 && s[i]<=90)
{
strcat(x,c[s[i++]-65]);
strcat(x," ");
}
else if(s[i]>=48 && s[i]<=57)
{
strcat(x,c[s[i++]-22]); /*26-35 is the number range of c[][]..*/
strcat(x," ");
}
else if(s[i]==32)
{
strcat(x,"/ ");
i++;
}
else
i++; /*i've only managed the alphabets and the numbers....*/
}
return x;
}
void toUpperCase(char s[])
{
int i=0;
while(s[i]!='\0')(s[i]>=97 && s[i]<=122)?s[i++]-=32:s[i++];
}