confusedcat
confusedcat

Reputation: 79

Morse Code Translator in C

We were supposed to translate a string into Morse code and I got that covered already using switch. Each letter is separated with a space but I don't know how to separate the words with a slash (/). Here is what I coded:

#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<process.h>
#include<ctype.h>

int main(){
char string[100], str1[100];
int length, i, j = 0;

printf("Enter sentence to convert: ");
gets(string);
length = strlen(string);

for (i = 0; i <= length; i++){
    switch(toupper(string[i])){
        case 'A':
            str1[j++]='.';
            str1[j++]='-';
            str1[j]=' ';
            break;

until Z and then...

        }
    j++;
}

str1[j - 1]='\0';
puts(str1);
system("pause");
return 0;
}

How do I add the slash to separate words if the string entered is a sentence?

Upvotes: 1

Views: 2227

Answers (1)

NPE
NPE

Reputation: 500427

Whenever you see a space (or a sequence of spaces?), append '/' to the output string.

Upvotes: 2

Related Questions