Caesar/caesar.cpp

40 lines
720 B
C++
Raw Normal View History

#include "caesar.hpp"
#include <string>
2024-06-13 16:37:44 +00:00
#include <iostream>
Caesar::Caesar(int rotation) {
setRotation(rotation);
}
void Caesar::setRotation(int rotation) {
this->rotation = rotation;
}
int Caesar::getRotation() {
return rotation;
}
char Caesar::encryptChar(char toEncrypt) {
2024-06-13 16:37:44 +00:00
int newChar = toEncrypt + rotation;
bool substractTwo = false;
2024-06-13 16:37:44 +00:00
while(newChar > MAX) newChar -= MAX;
2024-06-13 16:37:44 +00:00
substractTwo = newChar < MIN;
while(newChar < MIN) newChar += MIN;
2024-06-13 16:37:44 +00:00
if(substractTwo) newChar -= 2;
return (char)newChar;
}
std::string Caesar::encryptString(std::string toEncrypt) {
std::string encrypted = "";
for(int i = 0; i < toEncrypt.length(); i++) {
encrypted += encryptChar(toEncrypt[i]);
}
return encrypted;
}