This should get you started:
Code:
itsme@itsme:~/C$ cat morse.c
#include <stdio.h>
#include <string.h>
#include <ctype.h> /* For isalpha() and toupper() */
char *ch_to_morse(char ch)
{
static char buf[5];
char *start, *p;
char MorseData[] = ".- -...-.-.-.. . ..-.--. ...... .----.- .-..-- -. --- .--.--.-.-. ... - ..- ...-.-- -..--.----..";
if(!isalpha(ch)) /* If it's not a letter return empty string */
{
buf[0] = '\0';
return buf;
}
/* Calculate where in MorseData to pull from */
start = MorseData + ((toupper(ch) - 'A') * 4);
strncpy(buf, start, 4); /* Copy 4 characters of MorseData */
buf[4] = '\0'; /* Terminate the string */
/* Get rid of trailing spaces */
if((p = strchr(buf, ' ')))
*p = '\0';
return buf;
}
int main(void)
{
char input[] = "Something the user typed";
int i;
for(i = 0;input[i];++i)
printf("%s ", ch_to_morse(input[i]));
putchar('\n');
return 0;
}
Code:
itsme@itsme:~/C$ ./morse
... --- -- . - .... .. -. --. - .... . ..- ... . .-. - -.-- .--. . -..
itsme@itsme:~/C$
The alphabet in ASCII is all laid out in order. So if you subract 'A' from 'B', for example, you get 1. 'A' from 'D' gives you '3'. So the calculation subtracts 'A' from the uppercase form of the input character and multiplies that answer by 4. That gives you where the code for that letter is in MorseData.