This program should cdo two things:
1: Convert a english phrase to morse code
2 convert a morse phrase to english
The english phrase should be taken and each letter seperated.
The position of the letter in the english array should be located and the correspomding position should be found in the morse array. The program should then print the morse code equivalent of the letters entered with spaces in between.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void encode();
void decode();
char english[]={"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 "};
char *morse[]={".-", "-...", "-.-.","-..", ".", "..-.", "--.", "....", "..",
".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-",
".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--",
"--..", ".----", "..---", "...--", "....-", ".....", "-....",
"--...", "---..", "----.", "----", " "};
main()
{ int choice;
do{
printf("\n Choose one of the following options");
printf("\n 1: Convert english to morse code");
printf("\n 2: Convert morse code to english ");
scanf("%d", &choice);
}while(choice<1 || choice>2);
if(choice==1)
encode();
if(choice=2)
decode();
getch();
return 0;
}
void encode()
{
char message[100]=" "; char code[200]="";
int c;
int z, x;
printf("\n Enter the phrase you wish to convert to morse: ");
scanf("%s", message);
c = strlen(message); //strlen finds the length of a string excluding
//the null token
for(z = 0; z <= c; z++) //the problem starts here
{
for(x = 0; english [x]!= message[z] && x < 37; x++); //while theposition english x
strcat(code, morse[x]); //is not equal to the position in morse x and x<37
strcat(code, " ");
}
printf("Code is %s", code);
getch();
}
void decode();
{
char string[200] = " ", eng[100] = " ";
char tokenptr;
int p;
printf("\n Enter the code you wish to convert to morse.");
printf("\n Use one blank space between each morse coded letter and");
printf("\n three blank spaces between each morse coded word");
scanf("%s", string);
tokenptr = strtok(string, " ");
while(tokenptr != NULL)
{
printf("%s\n", tokenPtr);
tokenPtr = strtok(NULL, " "); //the call to string should continue breaking up the string
} //from the location in string saved by the last token
getch();
}
All your help will be appreciated!!!!!