Code:
/* MorseCode.c
* -----------
* This program converts Morse code characters
* to their letter equivalents.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int search(const char *a[], const char *key, int size);
int main()
{
int element;
int size=5;
/* morse codes of the 26 letters */
char *morse[26] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
char *letters[26] = { "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" };
char *next[5];
int v;
printf("Enter Morse codes separated by spaces (type EOF to quit)\n\n");
/* sentinel based loop -- the end of file marker is the sentinel */
while (scanf("%s", next) != EOF)
{
++v;
element = search(morse, next[5], size);
if (element != -1)
{
printf("%s %c\n", morse[v], letters[v]);
}
}
return 0;
}
int search(const char *a[], const char *key, int size)
{
int n;
for (n=0; n<=size-1 ; ++n)
{
strcmp(*a, key);
if (strcmp(*a,key) ==0)
return 0;
}
return -1;
}/* MorseCode.c
* -----------
* This program converts Morse code characters
* to their letter equivalents.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int search(const char *a[], const char *key, int size);
int main()
{
int element;
int size=5;
/* morse codes of the 26 letters */
char *morse[26] = {
".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."
};
char *letters[26] = { "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" };
char *next[5];
int v;
printf("Enter Morse codes separated by spaces (type EOF to quit)\n\n");
/* sentinel based loop -- the end of file marker is the sentinel */
while (scanf("%s", next) != EOF)
{
++v;
element = search(morse, next[5], size);
if (element != -1)
{
printf("%s %c\n", morse[v], letters[v]);
}
}
return 0;
}
int search(const char *a[], const char *key, int size)
{
int n;
for (n=0; n<=size-1 ; ++n)
{
strcmp(*a, key);
if (strcmp(*a,key) ==0)
return 0;
}
return -1;
}