![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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;
}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 |
|
|
|
|
|
#2 |
|
Professional Programmer
|
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;} |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jun 2005
Location: India, The great.
Posts: 435
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Sep 2005
Location: Slovenia
Posts: 17
Rep Power: 0
![]() |
This is ROT encryption.
More info: http://en.wikipedia.org/wiki/ROT13 |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: May 2005
Location: Woo - Boot Sector!
Posts: 294
Rep Power: 4
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|