Caesar cipher
Mar 6, 2014 at 8:14am UTC
The project was to write Caesar cipher using a 13-letter offset. I know i did it the long way and there is a possible shorter way. The problem is I can't seem to find the error in my code.A hint or any advice on how to solve the issue would be more than helpful.
the output after i type hello world is:
'Enter Sentence:
Your sentence is
hello world'
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
#include<iostream>
using namespace std;
void letterch(char s[]);
int main(){
char sentence[80];
cout<<"Enter Sentence:\n" <<endl;
cin.getline(sentence, 80);
cout<<"Your sentence is \n" <<sentence<<endl;
letterch(sentence);
cout<<"your sentence is now: \n" <<sentence<<endl;
return 0;
}
void letterch(char s[]){
int i = 0;
while (s[i]!= '\0' ) {
if (s[i] =='a' ){
s[i] ='n' ;}
else if (s[i] =='b' ){
s[i] ='o' ;}
else if (s[i] =='c' ){
s[i] ='p' ;}
else if (s[i] =='d' ){
s[i] ='q' ;}
else if (s[i] =='e' ){
s[i] ='r' ;}
else if (s[i] =='f' ){
s[i] ='s' ;}
else if (s[i] =='g' ){
s[i] ='t' ;}
else if (s[i] =='h' ){
s[i] ='u' ;}
else if (s[i] =='i' ){
s[i] ='v' ;}
else if (s[i] =='j' ){
s[i] ='w' ;}
else if (s[i] =='k' ){
s[i] ='x' ;}
else if (s[i] =='l' ){
s[i] ='y' ;}
else if (s[i] =='m' ){
s[i] ='z' ;}
else if (s[i] =='n' ){
s[i] ='a' ;}
else if (s[i] =='o' ){
s[i] ='b' ;}
else if (s[i] =='p' ){
s[i] ='c' ;}
else if (s[i] =='q' ){
s[i] ='d' ;}
else if (s[i] =='r' ){
s[i] ='e' ;}
else if (s[i] =='s' ){
s[i] ='f' ;}
else if (s[i] =='t' ){
s[i] ='g' ;}
else if (s[i] =='o' ){
s[i] ='h' ;}
else if (s[i] =='v' ){
s[i] ='i' ;}
else if (s[i] =='w' ){
s[i] ='j' ;}
else if (s[i] =='x' ){
s[i] ='k' ;}
else if (s[i] =='y' ){
s[i] ='l' ;}
else if (s[i] =='z' ){
s[i] ='m' ;}
// capital //
else if (s[i] =='A' ){
s[i] ='N' ;}
else if (s[i] =='B' ){
s[i] ='O' ;}
else if (s[i] =='C' ){
s[i] ='P' ;}
else if (s[i] =='D' ){
s[i] ='Q' ;}
else if (s[i] =='E' ){
s[i] ='R' ;}
else if (s[i] =='F' ){
s[i] ='S' ;}
else if (s[i] =='G' ){
s[i] ='T' ;}
else if (s[i] =='H' ){
s[i] ='U' ;}
else if (s[i] =='I' ){
s[i] ='V' ;}
else if (s[i] =='J' ){
s[i] ='W' ;}
else if (s[i] =='K' ){
s[i] ='X' ;}
else if (s[i] =='L' ){
s[i] ='Y' ;}
else if (s[i] =='M' ){
s[i] ='Z' ;}
else if (s[i] =='N' ){
s[i] ='A' ;}
else if (s[i] =='O' ){
s[i] ='B' ;}
else if (s[i] =='P' ){
s[i] ='C' ;}
else if (s[i] =='Q' ){
s[i] ='D' ;}
else if (s[i] =='R' ){
s[i] ='E' ;}
else if (s[i] =='S' ){
s[i] ='F' ;}
else if (s[i] =='T' ){
s[i] ='G' ;}
else if (s[i] =='O' ){
s[i] ='H' ;}
else if (s[i] =='V' ){
s[i] ='I' ;}
else if (s[i] =='W' ){
s[i] ='J' ;}
else if (s[i] =='X' ){
s[i] ='K' ;}
else if (s[i] =='Y' ){
s[i] ='L' ;}
else if (s[i] =='Z' ){
s[i] ='M' ;}
else {
i++;}
}
}
Mar 6, 2014 at 8:21am UTC
You should do ++i every time. Not in last else statement. Now your program finds 'H', changes it to 'U' and checks same letter again. This will lead to infinite loop. And also you forgot to check for u and U using 2 O instead
Mar 6, 2014 at 8:45am UTC
I fixed the errors and the program works. You even found an error that I quickly over look. Thank you!
Last edited on Mar 6, 2014 at 8:46am UTC
Topic archived. No new replies allowed.