Question
Create a program that encrypts and decrypts messages. The message should encrypt by rotating each alphabetical character by their position in the message. For example,
Create a program that encrypts and decrypts messages. The message should encrypt by "rotating" each alphabetical character by their position in the message. For example, the string "abc", should be encrypted to "bdf." Since the 'a' is the first character in the message, it is rotated 1 letter 'b'. The second letter, 'b', is rotated two positions to 'd' and 'c' the third letter is rotated three letters to 'f' So the word "zoo" is encrypted to "aqr" Notice that 'z' wrapped around to 'a'
This is what I have so far. Please try to only use #include
#include
int main ()
{
int i = 0;
char message[1000];
int ans;
char ch;
printf("Please enter your message:");
message[i] = getchar();
while (message[i] != ' ')
{
i = i +1;
message[i] = getchar();
}
message [i] = '\0';
printf(" %s",message);
printf("Would you like to (1) encrypt this message or (2) decrypt it : ");
scanf(" %i", &ans);
//encrypts message
if(ans == 1)
{
ch = message[i];
if( ch >= 'a' && ch <= 'z')
{
ch = ch + ans;
if (ch > 'z')
{
ch = ch - 'z' + 'a' -1;
}
message[i] = ch;
}
else if( ch >= 'A' && ch <= 'Z')
{
ch = ch + ans;
if (ch > 'Z')
{
ch = ch - 'Z' + 'A' -1;
}
message[i] = ch;
}
}
//decrypts message
if (ans == 2)
{
if (ch>= 'a' && ch <='z')
{
ch = ch - ans;
if (ch < 'a')
{
ch = ch + 'z' -'a' +1;
}
message[i] = ch;
}
else if( ch >= 'A' && ch <= 'Z')
{
ch = ch - ans;
if(ch < 'A')
{
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("message is: %s ", message);
}
if(ans == 1)
{
ch = message[i];
if( ch >= 'a' && ch <= 'z')
{
ch = ch + ans;
if (ch > 'z')
{
ch = ch - 'z' + 'a' -1;
}
message[i] = ch;
}
else if( ch >= 'A' && ch <= 'Z')
{
ch = ch + ans;
if (ch > 'Z')
{
ch = ch - 'Z' + 'A' -1;
}
message[i] = ch;
}
}
if (ans == 2)
{
if (ch>= 'a' && ch <='z')
{
ch = ch - ans;
if (ch < 'a')
{
ch = ch + 'z' -'a' +1;
}
message[i] = ch;
}
else if( ch >= 'A' && ch <= 'Z')
{
ch = ch - ans;
if(ch < 'A')
{
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
}
printf("message is: %s ", message);
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started