Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   modulo arithmetic question (http://www.programmingforums.org/showthread.php?t=6105)

LOI Kratong Sep 26th, 2005 3:44 PM

modulo arithmetic question
 
I am writing a program that deals with ciphers and codes. When the user enters a set of text and a number after being prompted, they are taken to this function:

:

for (i = 0; i < len; i++)
    {
        workingtext[i] = workingtext[i] + ikey; //add the key to the plaintext                                                                                                   
    }                                                                                                 
    return workingtext;
}

where len is the length of workingtext

I want the resulting workingtext to only be a letter a-z, not any of the wierd computer characters (e.g. smiley faces, musical notes and french circumflex accents (just some of my favourites)). I think that it probably just requires a modulo thingy in here somewhere, can some one indicate where?

iignotus Sep 26th, 2005 5:35 PM

ASCII normal printing characters are only d32 to d126. A-Z is d65-d90, and a-z is d97-d122. http://www.lookuptables.com/

InfoGeek Sep 27th, 2005 5:44 AM

assuming that the characters entered by the user lie between a-z, you can do
:

  workingtext[i] = 97+(workingtext[i] + ikey)%26;

DaWei Sep 27th, 2005 7:22 AM

You could also construct an array for your desired character set that would be filled in at runtime in a way sensitive to the locale, then apply your modulus function according to the length of the array. If you're a portability kind of person. I'm (perhaps unfortunately) not.

LOI Kratong Sep 27th, 2005 1:41 PM

Thanks guys, haven't tried it yet, but from what i can see it should do what i want so thanks...

snow Sep 27th, 2005 5:07 PM

This is ROT encryption.
More info: http://en.wikipedia.org/wiki/ROT13

LOI Kratong Sep 28th, 2005 3:07 AM

Ah! I tried it but it didn't work! If you try and use 1 as the key, it outputs with a key of 20!

Anyone know why?


All times are GMT -5. The time now is 5:35 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC