Guest User

My Solution

a guest
Feb 28th, 2020
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. std::string atbash(std::string str) {
  2.     std::string eString{};
  3.     std::vector<char> letters {'a','b','c','d','e','f','g','h','i','j','k','l','m',
  4.     'n','o','p','q','r','s','t','u','v','w','x','y','z'};
  5.     std::vector<char> uLetters {'A','B','C','D','E','F','G','H','I','J','K','L','M',
  6.     'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
  7.  
  8.     for(auto& c : str)
  9.     {
  10.         if(!isalpha(c) || c == ' ')
  11.         {
  12.             eString += c;
  13.             continue;
  14.         }
  15.         if(islower(c))
  16.         {
  17.             auto it = std::find(letters.begin(), letters.end(), c);
  18.             int index = std::distance(letters.begin(), it);
  19.             eString += letters.at(letters.size()-1 - index);
  20.         }
  21.         else if(isupper(c))
  22.         {
  23.             auto it = std::find(uLetters.begin(), uLetters.end(), c);
  24.             int index = std::distance(uLetters.begin(), it);
  25.             eString += uLetters.at(uLetters.size()-1 - index);
  26.         }  
  27.     }
  28.     return eString;
  29. }
Advertisement
Add Comment
Please, Sign In to add comment