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 ...
- 09-27-2008 #1Just 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:
And I have several problems: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(c >= 'A' && c <= 'Z')
return c - 'A' + 10;
else if(c >= '0' && c <= '9')
return c - '0';
else return 1;
}
char code_to_char(short c)
{
if(c >= 0 && c <= 9)
return c + '0';
else if(c >= 10 && c <= 35)
return c - 10 + 'A';
}
char encode(char c, char k)
{
return code_to_char((char_to_code(c) + char_to_code(k)) % CODE_BASE);
}
char decode (char c, char 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 argc, char* argv[])
{
if (argc == 2 && strcmp("-h", argv[1]) == 0)
{
printHelp();
return 0;
}
else if (argc == 3 && strcmp("-encode", argv[1]) == 0)
{
int key_length = strlen(argv[2]);
int key_counter = 0;
int c;
while((c = getchar()) != EOF)
{
putchar(encode((char)c, argv[2][key_counter%key_length]));
key_counter++;
}
return EXIT_SUCCESS;
}
else if (argc == 3 && strcmp("-decode", argv[1]) == 0)
{
int key_length = strlen(argv[2]);
int key_counter = 0;
int c;
while((c = getchar()) != EOF)
{
putchar(decode((char)c, argv[2][key_counter%key_length]));
key_counter++;
}
return EXIT_SUCCESS;
}
else printError(EPARAM);
}
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:
I should get this:prog -encode AAA
BBB
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:
I should get this:prog -decode AAA
LLL
BBB
(L-A = 21 - 10 = 11 = B)
but I get this:
§§§9
Any ideas how to fix this?
- 09-28-2008 #2Linux 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("%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


Reply With Quote