Sponsored Content
Top Forums Programming Programming Challenges - A List Post 302119092 by blowtorch on Monday 28th of May 2007 12:12:02 AM
Old 05-28-2007
Here's the C code implementation. It is working, but with a wierd output:
Code:
#include<stdio.h>
#include<string.h>
#include<ctype.h>

struct morse {
        char letter;
        char morseCode[6];
};

int main(int argc, char *argv[]) {
        struct morse morse[36];
        char input;

        morse[0].letter='0';
        strcpy(morse[0].morseCode,"-----");
        morse[1].letter='1';
        strcpy(morse[1].morseCode,".----");
        morse[2].letter='2';
        strcpy(morse[2].morseCode,"..---");
        morse[3].letter='3';
        strcpy(morse[3].morseCode,"...--");
        morse[4].letter='4';
        strcpy(morse[4].morseCode,"....-");
        morse[5].letter='5';
        strcpy(morse[5].morseCode,".....");
        morse[6].letter='6';
        strcpy(morse[6].morseCode,"-....");
        morse[7].letter='7';
        strcpy(morse[7].morseCode,"--...");
        morse[8].letter='8';
        strcpy(morse[8].morseCode,"---..");
        morse[9].letter='9';
        strcpy(morse[9].morseCode,"----.");
        morse[10].letter='A';
        strcpy(morse[10].morseCode,".-");
        morse[11].letter='B';
        strcpy(morse[11].morseCode,"-...");
        morse[12].letter='C';
        strcpy(morse[12].morseCode,"-.-.");
        morse[13].letter='D';
        strcpy(morse[13].morseCode,"-..");
        morse[14].letter='E';
        strcpy(morse[14].morseCode,".");
        morse[15].letter='F';
        strcpy(morse[15].morseCode,"..-.");
        morse[16].letter='G';
        strcpy(morse[16].morseCode,"--.");
        morse[17].letter='H';
        strcpy(morse[17].morseCode,"....");
        morse[18].letter='I';
        strcpy(morse[18].morseCode,"..");
        morse[19].letter='J';
        strcpy(morse[19].morseCode,".---");
        morse[20].letter='K';
        strcpy(morse[20].morseCode,"-.-");
        morse[21].letter='L';
        strcpy(morse[21].morseCode,".-..");
        morse[22].letter='M';
        strcpy(morse[22].morseCode,"--");
        morse[23].letter='N';
        strcpy(morse[23].morseCode,"-.");
        morse[24].letter='O';
        strcpy(morse[24].morseCode,"---");
        morse[25].letter='P';
        strcpy(morse[25].morseCode,".--.");
        morse[26].letter='Q';
        strcpy(morse[26].morseCode,"--.-");
        morse[27].letter='R';
        strcpy(morse[27].morseCode,".-.");
        morse[28].letter='S';
        strcpy(morse[28].morseCode,"...");
        morse[29].letter='T';
        strcpy(morse[29].morseCode,"-");
        morse[30].letter='U';
        strcpy(morse[30].morseCode,"..-");
        morse[31].letter='V';
        strcpy(morse[31].morseCode,"...-");
        morse[32].letter='W';
        strcpy(morse[32].morseCode,".--");
        morse[33].letter='X';
        strcpy(morse[33].morseCode,"-..-");
        morse[34].letter='Y';
        strcpy(morse[34].morseCode,"-.--");
        morse[35].letter='Z';
        strcpy(morse[35].morseCode,"--..");

        while((input=fgetc(stdin))!=EOF) {
                if(input=='\n') fprintf(stdout,"\n");
                if(input==' ') {
                        fprintf(stdout,"   ");
                }
                if(input>47&&input<58) {
                        fprintf(stdout,"%s ",morse[input-48].morseCode);
                        continue;
                }
                input=toupper(input);
                fprintf(stdout,"%s ",morse[input-55].morseCode);
        }

}

The letter in the struct is not used because I am manipulating the array index, but if you try to add symbols, it may be necessary to actually run through the entire array every time and compare the input value with the letter and print the corresponding morse code value. And I can't understand the output that I'm getting either.

Code:
# ./a.out < test
- .... .. ...    ÿ¿ü .. ...    ÿ¿ü .-    ÿ¿ü - . ... -    ÿ¿ü ..-. .. .-.. .    ÿ¿ü - . ... - .. -. --.    ÿ¿ü ..-. --- .-.    ÿ¿ü -- --- .-. ... .    ÿ¿ü -.-. --- -.. .    ÿ¿ü - .... .- -    ÿ¿ü .. ...    ÿ¿ü -- --- .-. ... .    ÿ¿ü .- -. -..    ÿ¿ü -. --- -    ÿ¿ü -- --- .-. --.. .

Why are those symbols popping up after every " " that is encountered?
 

3 More Discussions You Might Find Interesting

1. AIX

AIX 6.1 IDSLDAP Installation Challenges

Please bare with me, since I am new to AIX and LDAP. I am attempting to install idsldap server on our AIX 6.1 NIM server. I installed the following packages: root@nim(/)# lslpp -l|grep ldap db2_08_01.ldap 8.1.1.80 COMMITTED DB2 LDAP Support idsldap.clt64bit61.rte 6.1.0.17 COMMITTED... (6 Replies)
Discussion started by: ecollins
6 Replies

2. UNIX for Advanced & Expert Users

Challenges in finding and copying the block

Hi, I have a below challenging task where iam unable to find the block and copy the same into a file. I tried my luck,howver iam unable to reach the first and second step..Can anyone help me with a clue or with the commands so that i can give a try. 1. search the <number>9966993366</number>... (2 Replies)
Discussion started by: cskumar
2 Replies

3. Shell Programming and Scripting

Korn shell script - SQL statement challenges

Hi scripting experts. I have some coding challenges that I'm hoping you can help me out. I have one file#1 that contains the following sql statement that spans over multiple lines: sql Select /*+ use_has(a,b) */ * from customer a, customer_address b where a.id = b.id... (1 Reply)
Discussion started by: pchang
1 Replies
All times are GMT -4. The time now is 01:23 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy