i've written a simple code to convert text to morse..need some advise.
Hi guys,
It's me again :D. I've written a simple code to convert some text to morse code....
after few days of fooling around with C code, i realised i often stumbled on the usage of pointers. i've always asked myself: im dealing with strings. Should i use arrays? or use pointers? whenever i tried pointers, it failed, hence i tried the array method instead.... :confused: Seems that im still confused with basic concepts of pointers and strings..
Here are my codes. For your kind inputs and corrections please.. :D
I've some queries here:
1. Under the *toMorse(char s[]) function, i've declared a static variable x[1024] which will be returned by the function. It will store the morse conversion. How do I convert 'x' to use pointers? Because I do not want to limit 'x' to size of 1024. But when ever i tried to implement 'x' via pointer methods, i would be pulling my hair off....
2. I believe toUpper() function can be changed to using pointers stead.. I've pulled off a wad of hair earlier trying to use pointers..
I've not learn how to walk properly and yet I'm trying to fly.. :D
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++];
}