#include #include #include #define MAX 1024 void tm(void); void menu(void); int main(void) { menu(); /* poderia usar um getchar() aqui pra segurar o programa */ return 0; } void menu(void) { int num; char s[20]; printf("1 - Converter texto pra morse\n"); printf("2 - Converter morse pra texto\n"); printf("Opcao: "); while (1) { fgets(s, 20, stdin); if ( sscanf(s, "%d", &num) != 1 ) continue; if ( num == 1 ) { tm(); break; } else if ( num == 2 ) { printf("Funcao nao implementada\n"); continue; } else { printf("Opcao invalida\n"); continue; } } } void tm(void) { char *alfabeto = "abcdefghijklmnopqrstuvwxyz"; char morse[][5] = { ".-", "-...", "-.-.", "-..", ".", "..-.","--.","....", "..", ".---","-.-",".-..","--", "-.","---",".--.","--.-",".-.", "...","-","..-","...-",".--", "-..-","-.--","--.." }; char str[MAX], str2[MAX], *p; int i, j; str2[0] = '\0'; /* so pra garantir o funcionamento de strcat() */ printf("Entre com uma string:\n"); if ( fgets(str, MAX, stdin) == NULL ) { fprintf(stderr, "Voce ultrapassou o limite de caracteres permitidos nessa string\n"); fprintf(stderr, "Saindo do programa.\n"); exit(-1); } /* remover o newline que o fgets coloca na string */ if ( p = strchr(str, '\n') ) *p = 0; for (i = 0 ; str[i] ; i++) if ( strchr(alfabeto, tolower(str[i])) != NULL) { strcat(str2, morse[tolower(str[i])-'a']); strcat(str2, " "); } printf("[%s] => [%s]\n\n", str, str2); }