Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define STR 1000
void English_to_Morse()
{
int len, a, b;
char phr[STR];
char alpha[]="abcdefghijklmnopqrstuvwxyz1234567890";
char *morse[37]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--..",".----","..---",
"...--","....-",".....","-....","--...","---..","----.",
"-----"," "};
printf("\n\tEnter in a phrase in English for encrypting\n"
"\tor enter 'ctrl+z' to quit\n");
while(*phr!='\0')
{
scanf("%s", phr);
len=strlen(phr);
for(a=0; a<len; a++)
{
for(b=0; b<37; b++)
{
if(tolower(phr[a])==alpha[b])
printf("%s ", morse[b]);
}
}
}
}
void Morse_to_English()
{
int len, a, b, c;
char phr[STR];
char alpha[]="abcdefghijklmnopqrstuvwxyz1234567890";
char *morse[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
"-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
"..-","...-",".--","-..-","-.--","--..",".----","..---",
"...--","....-",".....","-....","--...","---..","----.",
"-----"};
printf("\n\tEnter a line of Morse Code for decrypting\n\n");
while(*phr!='\0')
{ //I struggled with getting the input to read all the characters before
//the space as 1 index so I came up with a for loop. It is supposed to
//read to the space and copy the contents of that string in the
//array index, but it didnt work. However, just like my other function, it will not read the null
// character and exit the loop.
scanf("%s", phr);
for(a=0; a<36; a++)
{
if(strcmp(phr, morse[a])==0)
printf("%c", alpha[a]);
}
}
}
int main()
{
char ch;
printf("\n\tSelect from the menu:\n"
"\tA. English to Morse\n"
"\tB. Morse to English\n");
scanf("%c", &ch);
switch(tolower(ch))
{
case 'a':
English_to_Morse();
break;
case 'b':
Morse_to_English();
break;
default:
break;
}
return 0;
}
I wrote my E_to_M function before figuring out the strcmp(), I know there is some cleaning up to do and I wil do that, right now my concern is the loop ending.