Advertisement
DasShelmer

7.s.2

Dec 2nd, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. char getCaesar(string* alphabet, char in, int shift) {
  6.     bool isUpper = isupper(in);
  7.     if (isUpper)// to lower case
  8.         in += 32;
  9.     int lenght = alphabet->length(), resindex;
  10.     if (shift > lenght)
  11.         shift %= lenght;
  12.     resindex = alphabet->find(in, 0);
  13.     if (resindex < 0)
  14.         return '_';
  15.     resindex += shift;
  16.     resindex %= lenght;
  17.  
  18.     return alphabet->at(resindex) + (isUpper? -32: 0);// return case back
  19. }
  20. int main() {
  21.     int shift = 0;
  22.     string alphabet = "abcde", newAlphabet, word;// always lower case
  23.     cout << "Enter new alphabet: ";
  24.     cin >> newAlphabet;
  25.     if (newAlphabet.length() > 0)
  26.         alphabet = newAlphabet;
  27.     cout << "Enter the caesar shift: ";
  28.     cin >> shift;
  29.     cout << "Enter word: ";
  30.     cin >> word;
  31.     for (int i = 0, lenght = word.length(); i < lenght; i++)
  32.         word[i] = getCaesar(&alphabet, word[i], shift);
  33.    
  34.     cout << "Result word: " << word << endl;
  35.     main();
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement