Find the answer to your Linux question:
Results 1 to 2 of 2
Hi guys, I've been trying to write a simple program which would decode and encode messages (program acts like the character is a number of 36 number system), I've experienced ...
  1. #1
    Just Joined!
    Join Date
    Mar 2008
    Posts
    12

    Simple Encoding/Decoding in C

    Hi guys,

    I've been trying to write a simple program which would decode and encode messages (program acts like the character is a number of 36 number system), I've experienced some problems and I would really appreciate any help in this matter.

    Here's the code I have so far:

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
     
    #define CODE_BASE 36

    enum terrors
    {
      
    ENUMINPUT,   /**< Wrong input. */
      
    EPARAM,     /**< Wrong parameters. */
      
    };

    const 
    char *EMSG[] =
    {
      
    "Incorrect character.\n"
        "Please use only letters A-Z and/or numbers 0-9."
    ,
      
    "Wrong parameters.\n"
        "For more info run the program with '-h' parameter."
    ,
    };

    int printError(int error)
    {
      
    fprintf(stderr"Error: %s\n\n"EMSG[error]);
      return 
    1;
    }
      
    short char_to_code(char c)
    {
        if(
    >= 'A' && <= 'Z')
            return 
    'A' 10;
        else if(
    >= '0' && <= '9')
            return 
    '0';
        else return 
    1;
    }
     
    char code_to_char(short c)
    {
        if(
    >= && <= 9)
            return 
    '0';
        else if(
    >= 10 && <= 35)
            return 
    10 'A';
    }
     
    char encode(char cchar k)
    {
        return 
    code_to_char((char_to_code(c) + char_to_code(k)) % CODE_BASE);
    }

    char decode (char cchar k)
    {
        return 
    code_to_char((char_to_code(c) - char_to_code(k)) % CODE_BASE);

     
    void printHelp(void)
    {
      
    printf("Program encodes/decodes text according to key.\n"
             "Use:\n"
             "          prog -h shows this help info.\n"
             "          prog -'encode' 'key' encodes text according to key.\n"
             "          prog -'decode' 'key' decodes text according to key.\n" 
            
    );
    }
     
    int main(int argccharargv[])
    {

    if (
    argc == && strcmp("-h"argv[1]) == 0)
        {
        
    printHelp();  
        return 
    0;
        }
    else if (
    argc == && strcmp("-encode"argv[1]) == 0)
       {    
        
    int key_length strlen(argv[2]);
        
    int key_counter 0;
        
    int c;  
        while((
    getchar()) != EOF)
        {
            
    putchar(encode((char)cargv[2][key_counter%key_length]));
            
    key_counter++;
        }
        return 
    EXIT_SUCCESS;
       }
    else if (
    argc == && strcmp("-decode"argv[1]) == 0)
       {    
        
    int key_length strlen(argv[2]);
        
    int key_counter 0;
        
    int c;  
        while((
    getchar()) != EOF)
        {
            
    putchar(decode((char)cargv[2][key_counter%key_length]));
            
    key_counter++;
        }
        return 
    EXIT_SUCCESS;
       }
    else 
    printError(EPARAM);

    And I have several problems:
    a) I'd like to make it so when incorrect character (only english letters A-Z and numbers 0-9 are allowed) is given to the program, an error message appears.

    b) when encoding, I always get one extra unwanted character in the end.
    example:
    prog -encode AAA
    BBB
    I should get this:
    LLL
    (A+B = 10 + 11 = 21 = L)
    but I get this:
    LLLB
    Any ideas how to fix this?

    c) decoding does not seem to work..
    example:
    prog -decode AAA
    LLL
    I should get this:
    BBB
    (L-A = 21 - 10 = 11 = B)
    but I get this:
    &#167;&#167;&#167;9
    Any ideas how to fix this?

  2. #2
    Linux Newbie
    Join Date
    Jan 2008
    Location
    UK
    Posts
    211
    Hi,
    For the input you could try using scanf, as with this you can use a scanset
    as it is known to accept only the allowed characters.

    such as:
    scanf("&#37;dt%d", &x, &y) with an input stream of 10t20

    it will store the 10 in x and 20 in y and the t is discarded.

    This:
    %[ABC]
    means only characters ABC will be read.

    Many times if you are getting extra characters is because you are dealing with a stream. When you decide which input method you are going to use check the manual on the function to see how it handles the data. You may have to either copy to another buffer with a manipulation on the input to drop the last character or set a loop after getting and or setting the input length.

    Hope this helps.

    wowbag1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...