203k views
2 votes
The Atbash Cipher encrypts messages by reversing lowercase letters, so ‘a’ becomes ‘z’, ‘b’ becomes ‘y’, ‘c’ becomes ‘x’, etc... Also, any space or punctuation mark gets repeated. For example, hello human! encrypts to svool sfnzm!! Encrypt msg and save the answer to a variable called encrypted (you don’t have to display anything). Note: msg will only have lowercase letters, punctuation and spaces. msg = input('Enter secret message: ', 's');

1 Answer

3 votes

Answer:

See Explanation Below

Step-by-step explanation:

// Program is written in C++ Programming Language

// Comments are used for explanatory purpose

// Program starts here

#include<iostream>

#include <bits/stdc++.h>

using namespace std;

int main()

{

// Declare 2 string variables to store the secret message and to store the encrypted text

string message, result;

// Prompt user to enter a secret message

cout<<"Enter a secret message: ";

cin>message;

// Convert the input string to char array

int n = message.length();

char char_array[n + 1];

strcpy(char_array, message.c_str());

// Initialise result

result = "";

// Declare an array of all possible alphabets a-z

char possible[26] = { 'a','b','c','d','e','f','g,','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',w','x','y','z'};

// Generate output string

// Start by getting string position

int count = 0;

while(count<n)

{

// If current character is blank or !

if(char_array[count] = '!' || char_array[count] = ' ')

{

result+=char_array[count];

}

else

{

for(int I = 0; I<26; I++)

{

if(char_array[count] = possible[I])

{

result+=possible[25-I];

}

}

}

count++;

}

// No output required; the program stops here

return 0;

}

// End of program

User Pedro Oliveira
by
4.7k points