Thread: i've written a simple code to convert text to morse..need some advise.

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    41

    i've written a simple code to convert text to morse..need some advise.

    Hi guys,

    It's me again . I've written a simple code to convert some text to morse code....

    after few days of fooling around with C code, i realised i often stumbled on the usage of pointers. i've always asked myself: im dealing with strings. Should i use arrays? or use pointers? whenever i tried pointers, it failed, hence i tried the array method instead.... Seems that im still confused with basic concepts of pointers and strings..

    Here are my codes. For your kind inputs and corrections please..

    I've some queries here:
    1. Under the *toMorse(char s[]) function, i've declared a static variable x[1024] which will be returned by the function. It will store the morse conversion. How do I convert 'x' to use pointers? Because I do not want to limit 'x' to size of 1024. But when ever i tried to implement 'x' via pointer methods, i would be pulling my hair off....

    2. I believe toUpper() function can be changed to using pointers stead.. I've pulled off a wad of hair earlier trying to use pointers..

    I've not learn how to walk properly and yet I'm trying to fly..


    Code:
    /*
    References: 
    http://en.wikipedia.org/wiki/Morse_code
    http://www.techonthenet.com/ascii/chart.php
    http://cprogramming.com 
    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   ----.
    Fullstop   .-.-.-
    Comma   --..--
    Query   ..--..
    Space /
    Period [.] 	ˇ - ˇ - ˇ -
    Comma [,] 	- - ˇ ˇ - -
    Question mark [?] 	ˇ ˇ - - ˇ ˇ
    Apostrophe ['] 	ˇ - - - - ˇ
    Exclamation mark [!] 	- ˇ - ˇ - -
    Slash [/] 	- ˇ ˇ - ˇ
    Parentheses ( ) 	- ˇ - - ˇ -
    Ampersand [&] 	ˇ ˇˇˇ
    Colon [:] 	- - - ˇ ˇ ˇ
    Semicolon [;] 	- ˇ - ˇ - ˇ
    Double dash [=] 	- ˇ ˇ ˇ -
    Fraction bar 	- ˇ ˇ - ˇ
    Hyphen [-] 	- ˇ ˇ ˇ ˇ -
    Underscore [_] 	ˇ ˇ - - ˇ -
    Quotation mark ["] 	ˇ - ˇ ˇ - ˇ
    "@" (commat) 	ˇ - - ˇ - ˇ
    */
    #include <stdio.h>
    
    void toUpperCase(char s[]);
    char *toMorse(char s[]);
    
    int main(int argc, char *argv[])
    {
      char *morse; 
      char s[]="sos call 911";
      toUpperCase(s);
      morse = toMorse(s);
      printf("%s",morse);
      return 0;
    }
    
    char *toMorse(char s[])
    {
    static char x[1024];
    char c[][37] = {
                    ".-",       /* 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*/
    		};
    int i=0;  
    while(s[i]!='\0')
    {
     if(s[i]>=65 && s[i]<=90)
     {
      strcat(x,c[s[i++]-65]);
      strcat(x," ");
     }
     else if(s[i]>=48 && s[i]<=57)
     {
      strcat(x,c[s[i++]-22]);      /*26-35 is the number range of c[][]..*/
      strcat(x," ");
     }
     else if(s[i]==32)
     {
      strcat(x,"/ ");
      i++;
     }
     else                                        
      i++;         /*i've only managed the alphabets and the numbers....*/
    } 
      return x; 
    }
    
    void toUpperCase(char s[])
    {
        int i=0;
        while(s[i]!='\0')(s[i]>=97 && s[i]<=122)?s[i++]-=32:s[i++];
    }
    Last edited by stevong; 11-20-2005 at 10:37 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Replies: 19
    Last Post: 05-30-2007, 05:47 PM
  3. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  4. Simple C++ question. Error in code somewhere.
    By Paracropolis in forum C++ Programming
    Replies: 10
    Last Post: 02-06-2006, 08:59 AM
  5. how convert english to morse code
    By uler in forum C++ Programming
    Replies: 2
    Last Post: 09-12-2001, 07:56 PM