Re: Mathematics of the Enigma cipher?



Michael Olea wrote:
CBFalconer wrote:
DarkProtoman wrote:

... snip ...
{
memcpy(Alphabet,"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",36);
}
int GetCurrentPos(){return CurPos;}
int CharacterMap(char Char)
{
if(Char='A')
return 0;
else if(Char='B')
return 1;

... snip ...

else if(Char='7')
return 34;
else if(Char='8')
return 35;
else if(Char='9')
return 36;
}

why all this code? Try (in C):

static const char *alphabet =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
const char *p;

if (p = strchr(alphabet, char)) return (p - alphabet) + 1;
else return 0; /* detect not in alphabet */

with which you can easily modify the alphabet in one place. Hint:
eschew code that requires various areas to agree if any change is
desired.

There is no need to store a string or loop over it, since in ASCII
uppercase alpha is contiguous, as are digits. So a version that
checks for invalid input:

Just as all computers are not x86s, all char sets are not ASCII.
Do you want your code to work, or do you want it to fail
mysteriously?

--
Merry Christmas, Happy Hanukah, Happy New Year
Joyeux Noel, Bonne Annee.
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>

.



Relevant Pages

  • Re: problems writing a printf/scanf function that handles both int and char input from the console
    ... > only exit by closing the command prompt window. ... snip unindented code with over long lines ... ... if you input values with scanf the char that terminates ... int flushln ...
    (comp.lang.c)
  • Re: stream io in c
    ... If you are using tabs to indent your code, consider switching to spaces, ... And note that 'c' is declared as an int, ... unsigned char. ...
    (comp.lang.c)
  • Re: int to string - without using stringstream.
    ... > When you cast an int to a char it basically means the char becomes the ascii ... correct acronym is ASCII. ... that do not use ASCII as the execution character set. ... Actually the char will contain the numeric value of the int, ...
    (alt.comp.lang.learn.c-cpp)
  • Re: function
    ... returns either the character passed (as an int) or EOF on error. ... It's just a byte with value zero. ... int parameter, cast to an unsigned char, to the stream indicated. ...
    (comp.lang.c)
  • Re: Defining the fields of a structure at run-time
    ... The fundamental problem in this part is nothing to do with the size of an int. ... Some real systems have alignment requirements for various types, so if you try to read a 2 byte integer from an odd address they will generate an error and abort your program. ... Since as far as the compiler is concerned a is an array of char a could start on an odd address. ...
    (comp.lang.c)