Homework- Morse Code output problem
I have been working on this for about 5 hours now and I can't figure this out.. Its my first semester in Computer Engineering with no programming experience.
So, the program is supposed to supply a space between each letter in morse code and 3 spaces between each word. However, the program is not displaying the first character in morse code letter, and randomly displays letters or symbols instead of the characters its supposed to. Here is the code. (also, I realize I don't need all the extra stuff like function calls, but I want to use them for practice)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STR 1000
void English_to_Morse(char *line, int size)
{
int j=0, a;
char str[STR];
for(a=0; a<size; a++)
{
switch(toupper(line[a]))
{
case 'A':
str[j]='.';
str[j++]='-';
str[j++]=' ';
break;
case 'B':
str[j]='-';
str[j++]='.';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case 'C':
str[j]='.';
str[j++]='-';
str[j++]='.';
str[j++]='-';
str[j++]=' ';
break;
case 'D':
str[j]='-';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case 'E':
str[j]='.';
break;
case 'F':
str[j]='.';
str[j++]='.';
str[j++]='-';
str[j++]='.';
str[j++]=' ';
break;
case 'G':
str[j]='-';
str[j++]='-';
str[j++]='.';
str[j++]=' ';
break;
case 'H':
str[j]='.';
str[j++]='.';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case 'I':
str[j]='.';
str[j++]='.';
str[j++]=' ';
break;
case 'J':
str[j]='.';
str[j++]='-';
str[j++]='-';
str[j++]='-';
str[j++]=' ';
break;
case 'K':
str[j]='.';
str[j++]='-';
str[j++]='.';
str[j++]=' ';
break;
case 'L':
str[j]='.';
str[j++]='-';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case 'M':
str[j]='-';
str[j++]='-';
str[j++]=' ';
break;
case 'N':
str[j]='-';
str[j++]='.';
str[j++]=' ';
break;
case 'O':
str[j]='-';
str[j++]='-';
str[j++]='-';
str[j++]=' ';
break;
case 'P':
str[j]='.';
str[j++]='-';
str[j++]='-';
str[j++]='.';
str[j++]=' ';
break;
case 'Q':
str[j]='-';
str[j++]='-';
str[j++]='.';
str[j++]='-';
str[j++]=' ';
break;
case 'R':
str[j]='.';
str[j++]='-';
str[j++]='.';
str[j++]=' ';
break;
case 'S':
str[j]='.';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case 'T':
str[j]='-';
str[j++]=' ';
break;
case 'U':
str[j]='.';
str[j++]='.';
str[j++]='-';
str[j++]=' ';
break;
case 'V':
str[j]='.';
str[j++]='.';
str[j++]='.';
str[j++]='-';
str[j++]=' ';
break;
case 'W':
str[j]='.';
str[j++]='-';
str[j++]='-';
str[j++]=' ';
break;
case 'X':
str[j]='-';
str[j++]='.';
str[j++]='.';
str[j++]='-';
str[j++]=' ';
break;
case 'Y':
str[j]='-';
str[j++]='-';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case 'Z':
str[j]='-';
str[j++]='-';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case '0':
str[j]='-';
str[j++]='-';
str[j++]='-';
str[j++]='-';
str[j++]='-';
str[j++]=' ';
break;
case '1':
str[j]='.';
str[j++]='.';
str[j++]='-';
str[j++]='-';
str[j++]='-';
str[j++]=' ';
break;
case '2':
str[j]='.';
str[j++]='.';
str[j++]='-';
str[j++]='-';
str[j++]='-';
str[j++]=' ';
break;
case '3':
str[j]='.';
str[j++]='.';
str[j++]='.';
str[j++]='-';
str[j++]='-';
str[j++]=' ';
break;
case '4':
str[j]='.';
str[j++]='.';
str[j++]='.';
str[j++]='.';
str[j++]='-';
str[j++]=' ';
break;
case '5':
str[j]='.';
str[j++]='.';
str[j++]='.';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case '6':
str[j]='_';
str[j++]='.';
str[j++]='.';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case '7':
str[j]='-';
str[j++]='-';
str[j++]='.';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case '8':
str[j]='-';
str[j++]='-';
str[j++]='-';
str[j++]='.';
str[j++]='.';
str[j++]=' ';
break;
case '9':
str[j]='-';
str[j++]='-';
str[j++]='-';
str[j++]='-';
str[j++]='.';
str[j++]=' ';
break;
case ' ':
str[j]=' ';
str[j++]=' ';
str[j++]=' ';
break;
}
j++;
}
printf("\n\t%s and size is %d\n", str, size-1);
}
int main()
{
int len;
char phr[STR];
printf("\n\tEnter a phrase or word you want to be converted to Morse Code\n"
"\tor a line of Morse Code you want to be converted to English\n\n");
printf("\t");
fgets(phr, STR, stdin);
len=strlen(phr);
English_to_Morse(phr, len);
return 0;
}