String to Morse Code - Copying from string to string

This is a discussion on String to Morse Code - Copying from string to string within the C Programming forums, part of the General Programming Boards category; Hi. this is my first post so Im sorry if it's in the wrong place. I havn't been programming in ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    Question String to Morse Code - Copying from string to string

    Hi. this is my first post so Im sorry if it's in the wrong place.

    I havn't been programming in C for very long but I'm already really liking it but I am stuck on a certain problem. For a simple text based adventure game Im writing (this may be a big project for a novice like me, but I feel that it's a good way to learn lots of different aspects of C while having fun at the same time). I want to be able to translate what a user types in, into morse code. I was going to use a switch statement for each letter etc but decided that was a very long way of doing this so I did some searching on Google and read stuff on the strncpy function but im having trouble with it. Ive got a charachter array that contains all morse code symbols, to make it more uniform I added spaces where necessary to make everything 4 charachters long (ie instead of ".-" for 'a' ive got ".- ")

    char MorseData[] = ".- -...-.-.-.. . ..-.--. ...... .----.- .-..-- -. --- .--.--.-.-. ... - ..- ...-.-- -..--.----..";

    Now depending on what the user enters I want to be able to translate those letters into there equivalent morse code. I was thinking something along the lines of converting the charachters into their Ascii code equivilent (which I havnt figured out yet) and then using a loop to convert them all into morse code but Ive been trying different things for so long I just cant do it. Once Ive got this (most likely basic thing) working I will then try to make it so the user can read a file in of either morse code or standard text and translate it but I figure that will also be a very simple (yet complicated thing for me) function and I will probably need further help.

    I am very grateful for any help I am given, thankyou.

    If this post doesnt make much sense I will try to answer any questions to clarify myself.

    Again, thanks for any help given.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    This should get you started:
    Code:
    itsme@itsme:~/C$ cat morse.c
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h> /* For isalpha() and toupper() */
    
    char *ch_to_morse(char ch)
    {
      static char buf[5];
      char *start, *p;
    
      char MorseData[] = ".-  -...-.-.-.. .   ..-.--. ......  .----.- .-..--  -.  --- .--.--.-.-. ... -   ..- ...-.-- -..--.----..";
    
      if(!isalpha(ch))  /* If it's not a letter return empty string */
      {
        buf[0] = '\0';
        return buf;
      }
    
      /* Calculate where in MorseData to pull from */
      start = MorseData + ((toupper(ch) - 'A') * 4);
      strncpy(buf, start, 4);  /* Copy 4 characters of MorseData */
      buf[4] = '\0';  /* Terminate the string */
    
      /* Get rid of trailing spaces */
      if((p = strchr(buf, ' ')))
        *p = '\0';
    
      return buf;
    }
    
    int main(void)
    {
      char input[] = "Something the user typed";
      int i;
    
      for(i = 0;input[i];++i)
        printf("%s ", ch_to_morse(input[i]));
      putchar('\n');
    
      return 0;
    }
    Code:
    itsme@itsme:~/C$ ./morse
    ... --- -- . - .... .. -. --.  - .... .  ..- ... . .-.  - -.-- .--. . -..
    itsme@itsme:~/C$
    The alphabet in ASCII is all laid out in order. So if you subract 'A' from 'B', for example, you get 1. 'A' from 'D' gives you '3'. So the calculation subtracts 'A' from the uppercase form of the input character and multiplies that answer by 4. That gives you where the code for that letter is in MorseData.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 03:13 PM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  5. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21