Code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
char *decodeMorse ( char *tokenPointer ) {
char *result = "?"; // some unknown morse sequence...
if((strcmp( tokenPointer,".-" ))==0)
result = "A";
if((strcmp( tokenPointer,"-..." ))==0)
result = "B";
return result;
}
int main () { // never void
char inputStr[256];
char outputStr[256];
char *morse;
char *ascii;
char *seps = " \n"; // spaces and newlines are separators
/* read a whole line of morse */
fgets( inputStr, sizeof(inputStr), stdin );
outputStr[0] = '\0'; // empty the string
/* split the line at each space */
for ( morse = strtok(inputStr,seps) ;
morse != NULL ;
morse = strtok(NULL,seps) ) {
ascii = decodeMorse( morse );
strcat( outputStr, " " );
strcat( outputStr, ascii );
}
printf( "%s\n",outputStr );
getchar( );
return 0; // all successful programs return 0
}