wrap around alphabet??

J

Jeffrey Barrett

hello, i'm working on a decoder program that lets the user input a key
(integer between 0 and 26) and then subtracts that number from a given
letter to produce a decoded message. for example, 'Y' - 12 = 'M'. My
problem is I would like the program to "wrap around the alphabet" when
it subtracts the key from the original letter. Ex. 'B' - 5 = 'W'.
this must work for lower and uppercase letters. any help would be
greatly appreciated.
 
T

Thomas Matthews

Jeffrey said:
hello, i'm working on a decoder program that lets the user input a key
(integer between 0 and 26) and then subtracts that number from a given
letter to produce a decoded message. for example, 'Y' - 12 = 'M'. My
problem is I would like the program to "wrap around the alphabet" when
it subtracts the key from the original letter. Ex. 'B' - 5 = 'W'.
this must work for lower and uppercase letters. any help would be
greatly appreciated.

Use your favorite search engine for "ROT-13".

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
J

Jack Klein

hello, i'm working on a decoder program that lets the user input a key
(integer between 0 and 26) and then subtracts that number from a given
letter to produce a decoded message. for example, 'Y' - 12 = 'M'. My
problem is I would like the program to "wrap around the alphabet" when
it subtracts the key from the original letter. Ex. 'B' - 5 = 'W'.
this must work for lower and uppercase letters. any help would be
greatly appreciated.

char upper_case [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";

Think about it, very useful in both directions.
 
J

Jeffrey Barrett

Martin Johansen said:
Try something like

#define AlphaWrap(c, n) ((c)-(n)<'A'?(int)'Z'+1+(c)-'A'-(n):(c)-(n))

thanks for the help

im very new to c programming, could you explain what the c and n
variables would be?

thanks
 
M

Martin Johansen

Try something like

#define AlphaWrap(c, n) ((c)-(n)<'A'?(int)'Z'+1+(c)-'A'-(n):(c)-(n))
 
R

Richard Bos

Martin Johansen said:
Try something like

#define AlphaWrap(c, n) ((c)-(n)<'A'?(int)'Z'+1+(c)-'A'-(n):(c)-(n))

You've tried this using EBCDIC, I suppose? Funny results, what?

Richard
 
A

Allan Bruce

Jeffrey Barrett said:
Martin Johansen <[email protected]> wrote in message

thanks for the help

im very new to c programming, could you explain what the c and n
variables would be?

thanks

It appears to be the char you want to encode, and the number of places to
shift for your encoding. e.g. to encode 'M' 12 spaces, then use
AlphaWrap('M', 12);
Allan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top