code.cpp 860 B

12345678910111213141516171819202122
  1. /* The Atbash cipher is an encryption method in which each letter
  2. * of a word is replaced with its "mirror" letter in the alphabet:
  3. * A <=> Z; B <=> Y; C <=> X; etc.
  4. * Create a function that takes a string and applies the Atbash cipher to it.
  5. * Capitalisation should be retained.
  6. * Non-alphabetic characters should not be altered.
  7. */
  8. std::string atbash(std::string str) {
  9. std::string normal = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  10. std::string bashed = "zyxwvutsrqponmlkjihgfedcbaZYXWVUTSRQPONMLKJIHGFEDCBA";
  11. for(int j = 0;j<str.size();j++){
  12. for(int i = 0;i<normal.size();i++){
  13. // check the string character with normal character
  14. if(str[j] == normal[i]){
  15. // a match has been found
  16. // replace found character with corresponding bashed character
  17. str[j] = bashed[i];break;
  18. }
  19. }
  20. }
  21. return str;
  22. }