another question from my c book

This is a discussion on another question from my c book within the C Programming forums, part of the General Programming Boards category; the letters of the alphabet A through Z can be represented in Morse code. Each letter is represented by a ...

  1. #1
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    Question another question from my c book

    the letters of the alphabet A through Z can be represented in Morse code. Each letter is represented by a combination of up to four dots (.) and/or dashes (-). For example

    A=".- ", B="-...", C="-.-.", D="-.. " through Z="--.."

    In these groups of characters spaces have been deliberately introduced to fill the code up to the maximum of four characters. However the space does not represent part of the code. The codes for the letters A through Z can be represented as the following character string.

    Code:
    char *MorseData = ".-  -...-.-.-.. .   ..-.--. ......  .----.- .-..--  "
                      "-.  --- .--.--.-.-. ... -   ..- ...-.-- -..--.----.."

    write a program to input a message string and using the character string MorseData, convert the message into Morse code and display the result.

    thats the question from the book, what i cant work out is how to link the string MorseData to the letters A to Z, i thought about using switch case with strcmp but would take a hell of lot of code.

    Also i seem to get thrown by questions like this, is it my in-experience with 'C', is one born with natural "logical" talents, or is a case of doing again and again till it sinks in.

    by the way i think this forum is wicked and find it much more helpfull than any resource at uni, when i got proficient i hope to give some thing back

    luigi

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Quote Originally Posted by luigi40
    the letters of the alphabet A through Z can be represented in Morse code. Each letter is represented by a combination of up to four dots (.) and/or dashes (-). For example

    A=".- ", B="-...", C="-.-.", D="-.. " through Z="--.."

    In these groups of characters spaces have been deliberately introduced to fill the code up to the maximum of four characters. However the space does not represent part of the code. The codes for the letters A through Z can be represented as the following character string.

    Code:
    char *MorseData = ".-  -...-.-.-.. .   ..-.--. ......  .----.- .-..--  "
                      "-.  --- .--.--.-.-. ... -   ..- ...-.-- -..--.----.."

    write a program to input a message string and using the character string MorseData, convert the message into Morse code and display the result.

    thats the question from the book, what i cant work out is how to link the string MorseData to the letters A to Z, i thought about using switch case with strcmp but would take a hell of lot of code.

    Also i seem to get thrown by questions like this, is it my in-experience with 'C', is one born with natural "logical" talents, or is a case of doing again and again till it sinks in.

    by the way i think this forum is wicked and find it much more helpfull than any resource at uni, when i got proficient i hope to give some thing back

    luigi
    Hi Luigi,

    what makes your life simpler is to declare the codes to an array of 26 chars, each of which is a 4-byte array. Like this...
    Code:
    char morsecode[26][4] = {".-  ","-...", and so on };
    Then, you parse the input string and just map the corresponding code onto the output string...
    Code:
     strcat(outputstring,morsecode[(*inputstring)-'A']);
    I assume that outputstring and inputstring are sufficiently large and the input string consists of letters in Upper Case. Modify it accordingly in case you get the string in lower or mixed case.

    Now, the rest is homework for you to solve...

    Happy programming,
    -Harsha.
    Help everyone you can

  3. #3
    budding software engineer luigi40's Avatar
    Join Date
    Jun 2004
    Location
    South Coast UK
    Posts
    61

    thanks but

    thank you for your prompt reply, however i am assuming as this chapter is on pointers that the char *MorseData has to remain as a single subscripted string (i could be wrong) pointed to by a char pointer variable.

    is there a not too difficult way to link the pointer based character array with the letters A to Z.

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    In that case

    Quote Originally Posted by luigi40
    thank you for your prompt reply, however i am assuming as this chapter is on pointers that the char *MorseData has to remain as a single subscripted string (i could be wrong) pointed to by a char pointer variable.

    is there a not too difficult way to link the pointer based character array with the letters A to Z.
    Well, in that case try...
    Code:
     strncpy(tempdest,*(MorseData+(*inputstring-'A')*4),4);
     strcat(deststring,tempdest);

    - Harsha
    Help everyone you can

  5. #5
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by vsriharsha
    what makes your life simpler is to declare the codes to an array of 26 chars, each of which is a 4-byte array. Like this...
    Code:
    char morsecode[26][4] = {".-  ","-...", and so on };
    Do you really want arrays of char that all are not valid strings? Probably not.
    Code:
    static char const morsecode[26][5] = {".-  ","-...", and so on };
    or more likely:
    Code:
    static char const * const morsecode[] = {".-  ","-...", and so on };
    Last edited by Emmanuel Delaha; 07-29-2004 at 09:25 AM. Reason: Typos
    Emmanuel Delahaye

    "C is a sharp tool"

  6. #6
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Quote Originally Posted by Emmanuel Delaha
    Dou really want arrays of char that all are not valid strings? Probably not.
    Code:
    static char const morsecode[26][5] = {".-  ","-...", and so on };
    or more likely:
    Code:
    static char const * const morsecode[] = {".-  ","-...", and so on };
    Oh,,, apologise for that mistake. And yes, const makes it more robust. But what is a static doing there? What way would it better the code than to make those strings allocated in the data section and making them local to the file/function in which it is declared (and I dont see a pressing need for it here).

    Cheers,
    Harsha.
    Help everyone you can

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok here is something that should help you. It was created in the "just woke up" stuper. There are several areas that can be improved but I'll leave that for you.
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int chartoint(char);
    void limitprint(char *, int);
    
    int main(void)
    {
      char *MorseData = ".-  -...-.-.-.. .   ..-.--. ......  .----.- .-..--  "
                        "-.  --- .--.--.-.-. ... -   ..- ...-.-- -..--.----..";
      char *ptr, *ptrcounter;
      char *msg = "Hello out there in cyberland";
    
      for (ptrcounter=msg; *ptrcounter; ptrcounter++)
      {
        ptr = MorseData + chartoint(*ptrcounter) * 4;
        limitprint(ptr, 4);
        putchar(' ');
      }
      putchar('\n');
      return 0;
    }
    
    int chartoint(char x)
    {
      const char alpha[] = "abcdefghijklmnopqrstuvwxyz";
      int counter;
      char temp = tolower((unsigned char)x);
      for (counter=0; counter<sizeof alpha-1; counter++)
        if ( temp == alpha[counter] )
          return counter;
    
      return -1;
    }
    
    void limitprint(char *str, int num)
    {
      char *ptr;
      for (ptr = str; ptr < str + num; ptr++)
        putchar(*ptr);
    }
    Last edited by Thantos; 07-29-2004 at 09:36 AM. Reason: Small mistake in code

  8. #8
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by vsriharsha
    But what is a static doing there? What way would it better the code than to make those strings allocated in the data section and making them local to the file/function in which it is declared (and I dont see a pressing need for it here).
    It makes no sense to have a const (read-only) array in the automatic memory. It belongs to the static memory.

    That said, it could be defined out of a function. In that case the 'static' only impacts the external linkage. If you need it, just remove the 'static'. It's my 'by-default' tuning.
    Emmanuel Delahaye

    "C is a sharp tool"

  9. #9
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192
    Quote Originally Posted by Emmanuel Delaha
    It makes no sense to have a const (read-only) array in the automatic memory. It belongs to the static memory.

    That said, it could be defined out of a function. In that case the 'static' only impacts the external linkage. If you need it, just remove the 'static'. It's my 'by-default' tuning.
    Oh.. Ok,,
    just found that Initialization of character arrays (strings) was permitted only on global and static variabls in the old K&R style, but was extended to automatic variables also in Ansi C. So, thought you were a prompt follower of K&R. Anyways, thanks for the info.

    Regards,
    Harsha
    Help everyone you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. new book about game programming using DirectX
    By Carlos in forum Game Programming
    Replies: 0
    Last Post: 09-20-2005, 08:30 AM
  2. K&R book question
    By caduardo21 in forum C Programming
    Replies: 1
    Last Post: 06-10-2005, 12:24 PM
  3. Question about the book "The C Programming Language"
    By caduardo21 in forum C Programming
    Replies: 4
    Last Post: 05-15-2005, 01:22 PM
  4. Input/output question
    By vice in forum C Programming
    Replies: 8
    Last Post: 04-27-2005, 08:17 AM
  5. Template question
    By grscot in forum C++ Programming
    Replies: 1
    Last Post: 04-29-2003, 03:12 PM

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