Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I was surprised to learn that there are not many are not many C++ Caesar ciphers on Code Review (3 others as of this question). And none that work in this fashion.

Here's the code:

// Caesar cipher
#include <iostream>
const char chars[] = {"abcdefghijklmnopqrstuvwxyz"};
// Find position in array
int findPosition(char x){
 int i;
 for(i = 0; i <= 25; i++){
 if(x == chars[i]){
 break;
 }
 }
 
 return i;
}
// Change char to next in array, loop back if at end
char nextChar(char x){
 if(findPosition(x) == 25){
 return chars[0];
 }
 return chars[findPosition(x) + 1];
}
// Rotate char desired amount
char rotChar(char x, int rotNum){
 for(int i = 1; i <= rotNum; i++){
 x = nextChar(x);
 }
 return x;
}
int main(){
 std::string str;
 std::getline(std::cin, str);
 std::cout << '\n';
 for(int i = 0; i <= 25; i++){
 std::cout << "ROT" << i << " - ";
 for(int k = 0; str[k] != '0円'; k++){
 std::cout << rotChar(str[k], i);
 }
 std::cout << '\n';
 }
 return 0;
}

This is a very simple Caesar cipher. It currently ignores anything uppercase or non-alphabetic.

Here's an example output:

ROT0 - helloworld
ROT1 - ifmmpxpsme
ROT2 - jgnnqyqtnf
ROT3 - khoorzruog
ROT4 - lippsasvph
ROT5 - mjqqtbtwqi
ROT6 - nkrrucuxrj
ROT7 - olssvdvysk
ROT8 - pmttwewztl
ROT9 - qnuuxfxaum
ROT10 - rovvygybvn
ROT11 - spwwzhzcwo
ROT12 - tqxxaiadxp
ROT13 - uryybjbeyq
ROT14 - vszzckcfzr
ROT15 - wtaadldgas
ROT16 - xubbemehbt
ROT17 - yvccfnficu
ROT18 - zwddgogjdv
ROT19 - axeehphkew
ROT20 - byffiqilfx
ROT21 - czggjrjmgy
ROT22 - dahhksknhz
ROT23 - ebiiltloia
ROT24 - fcjjmumpjb
ROT25 - gdkknvnqkc

How can this code be improved?

I was surprised to learn that there are not many C++ Caesar ciphers on Code Review (3 others as of this question). And none that work in this fashion.

Here's the code:

// Caesar cipher
#include <iostream>
const char chars[] = {"abcdefghijklmnopqrstuvwxyz"};
// Find position in array
int findPosition(char x){
 int i;
 for(i = 0; i <= 25; i++){
 if(x == chars[i]){
 break;
 }
 }
 
 return i;
}
// Change char to next in array, loop back if at end
char nextChar(char x){
 if(findPosition(x) == 25){
 return chars[0];
 }
 return chars[findPosition(x) + 1];
}
// Rotate char desired amount
char rotChar(char x, int rotNum){
 for(int i = 1; i <= rotNum; i++){
 x = nextChar(x);
 }
 return x;
}
int main(){
 std::string str;
 std::getline(std::cin, str);
 std::cout << '\n';
 for(int i = 0; i <= 25; i++){
 std::cout << "ROT" << i << " - ";
 for(int k = 0; str[k] != '0円'; k++){
 std::cout << rotChar(str[k], i);
 }
 std::cout << '\n';
 }
 return 0;
}

This is a very simple Caesar cipher. It currently ignores anything uppercase or non-alphabetic.

Here's an example output:

ROT0 - helloworld
ROT1 - ifmmpxpsme
ROT2 - jgnnqyqtnf
ROT3 - khoorzruog
ROT4 - lippsasvph
ROT5 - mjqqtbtwqi
ROT6 - nkrrucuxrj
ROT7 - olssvdvysk
ROT8 - pmttwewztl
ROT9 - qnuuxfxaum
ROT10 - rovvygybvn
ROT11 - spwwzhzcwo
ROT12 - tqxxaiadxp
ROT13 - uryybjbeyq
ROT14 - vszzckcfzr
ROT15 - wtaadldgas
ROT16 - xubbemehbt
ROT17 - yvccfnficu
ROT18 - zwddgogjdv
ROT19 - axeehphkew
ROT20 - byffiqilfx
ROT21 - czggjrjmgy
ROT22 - dahhksknhz
ROT23 - ebiiltloia
ROT24 - fcjjmumpjb
ROT25 - gdkknvnqkc

How can this code be improved?

I was surprised to learn that there are not many C++ Caesar ciphers on Code Review (3 others as of this question). And none that work in this fashion.

Here's the code:

// Caesar cipher
#include <iostream>
const char chars[] = {"abcdefghijklmnopqrstuvwxyz"};
// Find position in array
int findPosition(char x){
 int i;
 for(i = 0; i <= 25; i++){
 if(x == chars[i]){
 break;
 }
 }
 
 return i;
}
// Change char to next in array, loop back if at end
char nextChar(char x){
 if(findPosition(x) == 25){
 return chars[0];
 }
 return chars[findPosition(x) + 1];
}
// Rotate char desired amount
char rotChar(char x, int rotNum){
 for(int i = 1; i <= rotNum; i++){
 x = nextChar(x);
 }
 return x;
}
int main(){
 std::string str;
 std::getline(std::cin, str);
 std::cout << '\n';
 for(int i = 0; i <= 25; i++){
 std::cout << "ROT" << i << " - ";
 for(int k = 0; str[k] != '0円'; k++){
 std::cout << rotChar(str[k], i);
 }
 std::cout << '\n';
 }
 return 0;
}

This is a very simple Caesar cipher. It currently ignores anything uppercase or non-alphabetic.

Here's an example output:

ROT0 - helloworld
ROT1 - ifmmpxpsme
ROT2 - jgnnqyqtnf
ROT3 - khoorzruog
ROT4 - lippsasvph
ROT5 - mjqqtbtwqi
ROT6 - nkrrucuxrj
ROT7 - olssvdvysk
ROT8 - pmttwewztl
ROT9 - qnuuxfxaum
ROT10 - rovvygybvn
ROT11 - spwwzhzcwo
ROT12 - tqxxaiadxp
ROT13 - uryybjbeyq
ROT14 - vszzckcfzr
ROT15 - wtaadldgas
ROT16 - xubbemehbt
ROT17 - yvccfnficu
ROT18 - zwddgogjdv
ROT19 - axeehphkew
ROT20 - byffiqilfx
ROT21 - czggjrjmgy
ROT22 - dahhksknhz
ROT23 - ebiiltloia
ROT24 - fcjjmumpjb
ROT25 - gdkknvnqkc

How can this code be improved?

Tweeted twitter.com/StackCodeReview/status/815633243135426560
Updated code to match what I have locally
Source Link
esote
  • 3.8k
  • 2
  • 25
  • 44

I was surprised to learn that there are not many C++ Caesar ciphers on Code Review (3 others as of this question). And none that work in this fashion.

Here's the code:

// Caesar cipher
#include <iostream>
const char chars[] = {"abcdefghijklmnopqrstuvwxyz"};
// Find position in array
int findPosition(char x){
 int i;
 for(i = 0; i <= 25; i++){
 if(x == chars[i]){
 break;
 }
 }
 
 return i;
}
// Change char to next in array, loop back if at end
char nextChar(char x){
 if(findPosition(x) == 25){
 return chars[0];
 }
 return chars[findPosition(x) + 1];
}
// Rotate char desired amount
char rotChar(char x, int rotNum){
 for(int i = 1; i <= rotNum; i++){
 x = nextChar(x);
 }
 return x;
}
int main(){
 std::string str;
 std::getline(std::cin, str);
 std::cout << '\n';
 for(int i = 0; i <= 25; i++){
 std::cout << "ROT" << i << " - ";
 for(int k = 0; str[k] != '0円'; k++){
 std::cout << rotChar(str[k], i);
 }
 std::cout << '\n';
 }
 return 0;
}

This is a very simple Caesar cipher. It currently ignores anything uppercase or non-alphabetic.

Here's an example output:

ROT0 - helloworld
ROT1 - ifmmpxpsme
ROT2 - jgnnqyqtnf
ROT3 - khoorzruog
ROT4 - lippsasvph
ROT5 - mjqqtbtwqi
ROT6 - nkrrucuxrj
ROT7 - olssvdvysk
ROT8 - pmttwewztl
ROT9 - qnuuxfxaum
ROT10 - rovvygybvn
ROT11 - spwwzhzcwo
ROT12 - tqxxaiadxp
ROT13 - uryybjbeyq
ROT14 - vszzckcfzr
ROT15 - wtaadldgas
ROT16 - xubbemehbt
ROT17 - yvccfnficu
ROT18 - zwddgogjdv
ROT19 - axeehphkew
ROT20 - byffiqilfx
ROT21 - czggjrjmgy
ROT22 - dahhksknhz
ROT23 - ebiiltloia
ROT24 - fcjjmumpjb
ROT25 - gdkknvnqkc

How can this code be improved?

I was surprised to learn that there are not many C++ Caesar ciphers on Code Review (3 others as of this question). And none that work in this fashion.

Here's the code:

// Caesar cipher
#include <iostream>
char chars[] = {"abcdefghijklmnopqrstuvwxyz"};
// Find position in array
int findPosition(char x){
 int i;
 for(i = 0; i <= 25; i++){
 if(x == chars[i]){
 break;
 }
 }
 
 return i;
}
// Change char to next in array, loop back if at end
char nextChar(char x){
 if(findPosition(x) == 25){
 return chars[0];
 }
 return chars[findPosition(x) + 1];
}
// Rotate char desired amount
char rotChar(char x, int rotNum){
 for(int i = 1; i <= rotNum; i++){
 x = nextChar(x);
 }
 return x;
}
int main(){
 std::string str;
 std::getline(std::cin, str);
 std::cout << '\n';
 for(int i = 0; i <= 25; i++){
 std::cout << "ROT" << i << " - ";
 for(int k = 0; str[k] != '0円'; k++){
 std::cout << rotChar(str[k], i);
 }
 std::cout << '\n';
 }
 return 0;
}

This is a very simple Caesar cipher. It currently ignores anything uppercase or non-alphabetic.

Here's an example output:

ROT0 - helloworld
ROT1 - ifmmpxpsme
ROT2 - jgnnqyqtnf
ROT3 - khoorzruog
ROT4 - lippsasvph
ROT5 - mjqqtbtwqi
ROT6 - nkrrucuxrj
ROT7 - olssvdvysk
ROT8 - pmttwewztl
ROT9 - qnuuxfxaum
ROT10 - rovvygybvn
ROT11 - spwwzhzcwo
ROT12 - tqxxaiadxp
ROT13 - uryybjbeyq
ROT14 - vszzckcfzr
ROT15 - wtaadldgas
ROT16 - xubbemehbt
ROT17 - yvccfnficu
ROT18 - zwddgogjdv
ROT19 - axeehphkew
ROT20 - byffiqilfx
ROT21 - czggjrjmgy
ROT22 - dahhksknhz
ROT23 - ebiiltloia
ROT24 - fcjjmumpjb
ROT25 - gdkknvnqkc

How can this code be improved?

I was surprised to learn that there are not many C++ Caesar ciphers on Code Review (3 others as of this question). And none that work in this fashion.

Here's the code:

// Caesar cipher
#include <iostream>
const char chars[] = {"abcdefghijklmnopqrstuvwxyz"};
// Find position in array
int findPosition(char x){
 int i;
 for(i = 0; i <= 25; i++){
 if(x == chars[i]){
 break;
 }
 }
 
 return i;
}
// Change char to next in array, loop back if at end
char nextChar(char x){
 if(findPosition(x) == 25){
 return chars[0];
 }
 return chars[findPosition(x) + 1];
}
// Rotate char desired amount
char rotChar(char x, int rotNum){
 for(int i = 1; i <= rotNum; i++){
 x = nextChar(x);
 }
 return x;
}
int main(){
 std::string str;
 std::getline(std::cin, str);
 std::cout << '\n';
 for(int i = 0; i <= 25; i++){
 std::cout << "ROT" << i << " - ";
 for(int k = 0; str[k] != '0円'; k++){
 std::cout << rotChar(str[k], i);
 }
 std::cout << '\n';
 }
 return 0;
}

This is a very simple Caesar cipher. It currently ignores anything uppercase or non-alphabetic.

Here's an example output:

ROT0 - helloworld
ROT1 - ifmmpxpsme
ROT2 - jgnnqyqtnf
ROT3 - khoorzruog
ROT4 - lippsasvph
ROT5 - mjqqtbtwqi
ROT6 - nkrrucuxrj
ROT7 - olssvdvysk
ROT8 - pmttwewztl
ROT9 - qnuuxfxaum
ROT10 - rovvygybvn
ROT11 - spwwzhzcwo
ROT12 - tqxxaiadxp
ROT13 - uryybjbeyq
ROT14 - vszzckcfzr
ROT15 - wtaadldgas
ROT16 - xubbemehbt
ROT17 - yvccfnficu
ROT18 - zwddgogjdv
ROT19 - axeehphkew
ROT20 - byffiqilfx
ROT21 - czggjrjmgy
ROT22 - dahhksknhz
ROT23 - ebiiltloia
ROT24 - fcjjmumpjb
ROT25 - gdkknvnqkc

How can this code be improved?

Source Link
esote
  • 3.8k
  • 2
  • 25
  • 44

Simple Caesar Cipher C++

I was surprised to learn that there are not many C++ Caesar ciphers on Code Review (3 others as of this question). And none that work in this fashion.

Here's the code:

// Caesar cipher
#include <iostream>
char chars[] = {"abcdefghijklmnopqrstuvwxyz"};
// Find position in array
int findPosition(char x){
 int i;
 for(i = 0; i <= 25; i++){
 if(x == chars[i]){
 break;
 }
 }
 
 return i;
}
// Change char to next in array, loop back if at end
char nextChar(char x){
 if(findPosition(x) == 25){
 return chars[0];
 }
 return chars[findPosition(x) + 1];
}
// Rotate char desired amount
char rotChar(char x, int rotNum){
 for(int i = 1; i <= rotNum; i++){
 x = nextChar(x);
 }
 return x;
}
int main(){
 std::string str;
 std::getline(std::cin, str);
 std::cout << '\n';
 for(int i = 0; i <= 25; i++){
 std::cout << "ROT" << i << " - ";
 for(int k = 0; str[k] != '0円'; k++){
 std::cout << rotChar(str[k], i);
 }
 std::cout << '\n';
 }
 return 0;
}

This is a very simple Caesar cipher. It currently ignores anything uppercase or non-alphabetic.

Here's an example output:

ROT0 - helloworld
ROT1 - ifmmpxpsme
ROT2 - jgnnqyqtnf
ROT3 - khoorzruog
ROT4 - lippsasvph
ROT5 - mjqqtbtwqi
ROT6 - nkrrucuxrj
ROT7 - olssvdvysk
ROT8 - pmttwewztl
ROT9 - qnuuxfxaum
ROT10 - rovvygybvn
ROT11 - spwwzhzcwo
ROT12 - tqxxaiadxp
ROT13 - uryybjbeyq
ROT14 - vszzckcfzr
ROT15 - wtaadldgas
ROT16 - xubbemehbt
ROT17 - yvccfnficu
ROT18 - zwddgogjdv
ROT19 - axeehphkew
ROT20 - byffiqilfx
ROT21 - czggjrjmgy
ROT22 - dahhksknhz
ROT23 - ebiiltloia
ROT24 - fcjjmumpjb
ROT25 - gdkknvnqkc

How can this code be improved?

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /