Rot 13 Help

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 36
Reputation: yasaswyg has a little shameless behaviour in the past 
Solved Threads: 0
yasaswyg yasaswyg is offline Offline
Light Poster

Rot 13 Help

 
0
  #1
33 Days Ago
I made a program that finds the ROT 13, but theres a little thing thats messing it up for me. Its supposed to find the ROT 13 of each character but when i run it, it gives me some funky character in return
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int rot13(int c)
  5. {
  6. if (c >= 'A' && c < 'N')
  7. {
  8. return c + 13;
  9. }
  10. if (c >= 'N' && c <= 'Z')
  11. {
  12. return c - 13;
  13. }
  14. if (c >= 'a' && c < 'n')
  15. {
  16. return c + 13;
  17. }
  18. if (c >= 'n' && c <= 'z')
  19. {
  20. return c - 13;
  21. }
  22.  
  23. }
  24. int main(void)
  25. {
  26. int c;
  27. printf("Enter a character: ");
  28.  
  29. while((c == getchar())!= EOF)
  30. {
  31. char a = rot13(c);
  32. printf("%c", a);
  33. c++;
  34. }
  35. return 0;
  36. }
Can some one help me out please
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #2
33 Days Ago
while((c = getchar())!= EOF)
And what does your function return for non-alpha input?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: yasaswyg has a little shameless behaviour in the past 
Solved Threads: 0
yasaswyg yasaswyg is offline Offline
Light Poster
 
0
  #3
32 Days Ago
Originally Posted by Dave Sinkula View Post
while((c = getchar())!= EOF)
And what does your function return for non-alpha input?
It just returns the number
if I input 10, it returns 10
if I input ? it returns ?

This program is supposed to do only one of the characters
If i input Apple, its supposed to return N and nothing else
Last edited by yasaswyg; 32 Days Ago at 7:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #4
32 Days Ago
Originally Posted by yasaswyg View Post
It just returns the number
if I input 10, it returns 10
if I input ? it returns ?
I don't see that return c; line.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 36
Reputation: yasaswyg has a little shameless behaviour in the past 
Solved Threads: 0
yasaswyg yasaswyg is offline Offline
Light Poster
 
0
  #5
32 Days Ago
Originally Posted by Dave Sinkula View Post
I don't see that return c; line.
I put that in.

my new code looks like this
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4.  
  5. int rot13(int c)
  6. {
  7. if (c >= 'A' && c < 'N')
  8. {
  9. return c + 13;
  10. }
  11. if (c >= 'N' && c <= 'Z')
  12. {
  13. return c - 13;
  14. }
  15. if (c >= 'a' && c < 'n')
  16. {
  17. return c + 13;
  18. }
  19. if (c >= 'n' && c <= 'z')
  20. {
  21. return c - 13;
  22. }
  23. return c;
  24. }
  25. int main(void)
  26. {
  27. int c;
  28. printf("ROT 13 Converter\n\nEnter a Character: ");
  29.  
  30. while((c = getchar())!= EOF)
  31. {
  32. char a = rot13(c);
  33. printf("%c", a);
  34. c++;
  35. }
  36. return 0;
  37. }
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC