Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Sep 26th, 2005, 2:44 PM   #1
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
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?
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Sep 26th, 2005, 4:35 PM   #2
iignotus
Professional Programmer
 
iignotus's Avatar
 
Join Date: Apr 2005
Location: Nowhere Special
Posts: 466
Rep Power: 4 iignotus is on a distinguished road
Send a message via AIM to iignotus
ASCII normal printing characters are only d32 to d126. A-Z is d65-d90, and a-z is d97-d122. http://www.lookuptables.com/
__________________
% rc4 hexkey < input > output
#define S ,t=s[i],s[i]=s[j],s[j]=t /* rc4 hexkey <file */
unsigned char k[256],s[256],i,j,t;main(c,v,e)char**v;{++v;while(++i)s[ 
i]=i;for(c=0;*(*v)++;k[c++]=e)sscanf((*v)++-1,"%2x",&e);while(j+=s[i]
+k[i%c]S,++i);for(j=0;c=~getchar();putchar(~c^s[t+=s[i]]))j+=s[++i]S;}
iignotus is offline   Reply With Quote
Old Sep 27th, 2005, 4:44 AM   #3
InfoGeek
Professional Programmer
 
InfoGeek's Avatar
 
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4 InfoGeek is on a distinguished road
assuming that the characters entered by the user lie between a-z, you can do
   workingtext[i] = 97+(workingtext[i] + ikey)%26;
__________________
PFO - My daily dose of technology.
InfoGeek is offline   Reply With Quote
Old Sep 27th, 2005, 6:22 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Sep 27th, 2005, 12:41 PM   #5
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
Thanks guys, haven't tried it yet, but from what i can see it should do what i want so thanks...
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Old Sep 27th, 2005, 4:07 PM   #6
snow
Newbie
 
Join Date: Sep 2005
Location: Slovenia
Posts: 17
Rep Power: 0 snow is on a distinguished road
This is ROT encryption.
More info: http://en.wikipedia.org/wiki/ROT13
snow is offline   Reply With Quote
Old Sep 28th, 2005, 2:07 AM   #7
LOI Kratong
Professional Programmer
 
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4 LOI Kratong is on a distinguished road
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?
__________________
www.heldtogether.co.uk
LOI Kratong is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:11 PM.

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