Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,149,836 members, 7,806,356 topics. Date: Tuesday, 23 April 2024 at 03:16 PM |
Nairaland Forum / Science/Technology / Programming / Caeser Cipher (2057 Views)
Caeser Cipher by Temas: 7:29pm On Feb 22, 2013 |
hello house, i have a project am working on presently, its about using C language to encrypt data. the code has been written and working but my problem is this, if i need to encrypt "how are you today" the program only takes the 1st string and encrypt it (how) and neglect the rest, but if the strings are written together like this "howareyoutoday", the program will encrypt it perfectly. please C gurus in the house help!!! |
Re: Caeser Cipher by Javanian: 8:40pm On Feb 22, 2013 |
Paste your code here... |
Re: Caeser Cipher by Temas: 9:19pm On Feb 22, 2013 |
// cesar cipher in c // code by Temas84 #include <stdio.h> #include <string.h> #define SIZE 1000 int main() { int a,k; printf("CAESAR CIPHER CODED BY TEMAS\n\n" char ourinput[SIZE]; int count=0; printf("Please Enter Your text to be Encrypted Or Decrypted\n\n" scanf("%s",&ourinput); printf("Please Choose 1 for Encrypting OR 2 for Decrypting\n\n" scanf("%d",&a); printf("please Enter the number of shift you want\n" scanf("%d",&k); switch (a) { case 1: for(count=0;count<SIZE;count++) { int i; if (isalpha(ourinput[count])) { ourinput[count]= tolower(ourinput[count]); for (i=0;i<k;i++) { if (ourinput[count]=='z') ourinput[count]='a'; else ourinput[count]++; } } } printf("The encrypted data is %s", ourinput); exit(0); break; case 2: for(count=0;count<SIZE;count++) { int i; if (isalpha(ourinput[count])) { ourinput[count]= tolower(ourinput[count]); for (i=0;i<k;i++) { if (ourinput[count]=='a') ourinput[count]='z'; else ourinput[count]--; } } } printf("The Decrypted data is %s", ourinput); exit(1); break; default: printf("wrong selection\n" exit(3); } printf("The encrypted data is %s", ourinput); getchar(); return 10; } |
Re: Caeser Cipher by Javanian: 10:29pm On Feb 22, 2013 |
Temas: the program only takes the 1st string and encrypt it (how) and neglect the rest, but if. First of all some methods/functions are missing in your program. BUT i think this happens because a 'white space' is not seen as a character and you are using a char[]. So it stops the encryption when it reaches the white space. I would have loved to see what isalpha() does... OPTION 1: use a String[] instead of a char[] OPTION 2 convert the String to a char[] before encryption But it all depends on what the isalpha() does...Hope this helps... |
Re: Caeser Cipher by Javanian: 10:41pm On Feb 22, 2013 |
You can also try something like this... if (ourinput[count]==' ') |
Re: Caeser Cipher by Temas: 10:45pm On Feb 22, 2013 |
Thanks for your response, am using the isalpha to make sure the input or text are all alphabet. Will try ur suggestion and get back. |
Re: Caeser Cipher by lordZOUGA(m): 10:51pm On Feb 22, 2013 |
your problem is most likely from isalpha(). |
Re: Caeser Cipher by Javanian: 10:53pm On Feb 22, 2013 |
Temas: Thanks for your response, am using the isalpha to make sure the input or text are all alphabet. Will try ur suggestion and get back. Your welcome but my previous suggestions may not work if isAlpha() does not see a white space as a character... |
Re: Caeser Cipher by Temas: 12:17am On Feb 23, 2013 |
thanks javanian finally resolved it. the issue has nothing to do with isalpha(); after stuffing the net to resolve this , i came across another syntax for getting input from the user "gets()" .i just need to read more on when to use scanf() and gets(). the get() reads white space character in between strings while scanf sees it as a null terminator(from my observation). or better still you can enlighten me on this if you have an idea. am beginning to appreciate c++ more cos with "cin()" i wont have this problem. really appreciate your time |
Re: Caeser Cipher by Javanian: 12:25am On Feb 23, 2013 |
Your welcome, happy you solved it... |
Re: Caeser Cipher by lordZOUGA(m): 2:36am On Feb 23, 2013 |
Temas: thanks javaniancin is not a function and whitespace is not a null terminator |
Re: Caeser Cipher by Temas: 8:31am On Feb 23, 2013 |
lordZOUGA: hey, i would appreciate when you criticize and give a concrete advise.not criticize to fell like a god writing "cin" like a function is a typo mistake. typical nigerian mentality |
Re: Caeser Cipher by lordZOUGA(m): 4:01pm On Feb 23, 2013 |
Temas:I see. cin is still not a function. that you heard it from a nigerian is irrelevant.. |
Re: Caeser Cipher by Javanian: 4:51pm On Feb 23, 2013 |
@lordZOUGA are you on whatsapp or facebook?? |
Re: Caeser Cipher by lordZOUGA(m): 8:02pm On Feb 23, 2013 |
Javanian: @lordZOUGA are you on whatsapp or facebook??yes, I use both. whatsapp mostly. |
Re: Caeser Cipher by Javanian: 8:17pm On Feb 23, 2013 |
Pls Can you P.M your number or email address or both?? |
Re: Caeser Cipher by lordZOUGA(m): 8:31pm On Feb 23, 2013 |
Javanian: Pls Can you P.M your number or email address or both??nairaland has a weird PM system. |
Re: Caeser Cipher by Javanian: 9:25pm On Feb 23, 2013 |
Thanks...you can take it off if you wish... |
Re: Caeser Cipher by AZeD1(m): 12:04am On Feb 24, 2013 |
Good for you op, Javanian There is nothing like String in C #I know you are into java, i'm just saying |
Re: Caeser Cipher by AZeD1(m): 12:06am On Feb 24, 2013 |
lordZOUGA:I see you are neck deep in C, give a brother some advice. I know the basics but the language is fun |
Re: Caeser Cipher by Javanian: 12:17am On Feb 24, 2013 |
A-ZeD: Thanks for the correction... |
Re: Caeser Cipher by AZeD1(m): 12:27am On Feb 24, 2013 |
Javanian:You welcome. What are you doing up so late? wanna put me through some java? |
Re: Caeser Cipher by Javanian: 12:34am On Feb 24, 2013 |
my bed time starts around 2 a.m. ...For the java am ready when you are... |
Re: Caeser Cipher by AZeD1(m): 12:36am On Feb 24, 2013 |
Javanian: my bed time starts around 2 a.m. ...For the java am ready when you are...Ok. Wont hurt to learn something from you. Can i IM you? |
Re: Caeser Cipher by Javanian: 12:39am On Feb 24, 2013 |
No Problem... |
Re: Caeser Cipher by AZeD1(m): 12:44am On Feb 24, 2013 |
give me your favorite IM platform |
Re: Caeser Cipher by Javanian: 12:48am On Feb 24, 2013 |
I use gmail, but just P.M. via Nairaland, we can work from there.. |
Re: Caeser Cipher by AZeD1(m): 12:50am On Feb 24, 2013 |
Sent a PM |
Re: Caeser Cipher by ToyinDipo(m): 12:02am On Feb 25, 2013 |
@javanian, u seem to be a java freak, I hope we could meet online and talk |
(1) (Reply)
Free Content Curation Plugin / Coldfusion Vs Php Code Battle / Vb.Net Tips and Tricks
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 31 |