Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- char getCaesar(string* alphabet, char in, int shift) {
- bool isUpper = isupper(in);
- if (isUpper)// to lower case
- in += 32;
- int lenght = alphabet->length(), resindex;
- if (shift > lenght)
- shift %= lenght;
- resindex = alphabet->find(in, 0);
- if (resindex < 0)
- return '_';
- resindex += shift;
- resindex %= lenght;
- return alphabet->at(resindex) + (isUpper? -32: 0);// return case back
- }
- int main() {
- int shift = 0;
- string alphabet = "abcde", newAlphabet, word;// always lower case
- cout << "Enter new alphabet: ";
- cin >> newAlphabet;
- if (newAlphabet.length() > 0)
- alphabet = newAlphabet;
- cout << "Enter the caesar shift: ";
- cin >> shift;
- cout << "Enter word: ";
- cin >> word;
- for (int i = 0, lenght = word.length(); i < lenght; i++)
- word[i] = getCaesar(&alphabet, word[i], shift);
- cout << "Result word: " << word << endl;
- main();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement