Code:
/* Prepocessor Directives and Macros */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define Max_Size 80
char morsecode[45][8] = {
"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--..","0-----",
"1.----","2..---","3...--","4....-","5.....","6-....","7--...",
"8---..","9----.","..-.-.-",",--..--",":---...","?..--..","'.----.",
"--....-","/-..-.","(-.--.-","\".-..-."};
main() {
FILE *fin,*fout;
fin = fopen("morse.txt","r");
fout = fopen("morse_out.txt","w");
char string1[Max_Size + 1]; /* This is used to store the output lines */
char string2[Max_Size];
char word1[Max_Size];
char word2[Max_Size];
int x; /* used to check if strings are in morse code */
int is_Morse; /* used to check if strings are in morse code */
int length; /* keeps length in check */
int k; /* counting */
int a;
int b;
int c;
int n;
int j;
int count;
while ( fgets( string1, Max_Size + 1, fin ) != NULL )
{
fprintf(fout,"Input: ");
fputs(string1, fout); /* For echo check */
length = strlen( string1);
/***** Loop that checks is string is Morse Code or English ****/
x = 0;
is_Morse = 1;
do {
if (string1[x] != '-' && string1[x] != '.' && string1[x] != ' ' && string1[x] != '\n' && string1[x] != '\0') {
is_Morse = 0;
}
x++;
} while (x <= strlen(string1) && is_Morse == 1);
/************ Line is checked either Morse or English **************/
if( is_Morse == 1 ) {
a = 1;
b = 2;
c = 3;
n = 0;
count = 0;
do {
if ( string1[a] == ' ' && string1[b] == ' ' && string1[c] == ' ') {
j = n+1;
count = 0;
while( (a-j) <= n ) {
word1[ (count) ] = string1[ (a-j) ];
count += 1;
j = j - 1;
} /* Closes while loop */
fprintf(fout,"word: %s \n",word1);
memset(word1, 0, Max_Size);
} /* Closes if loop */
a++; b++; c++; n++; } /* Closes do loop */
while( string1[c] != '\0' );
}}
}